我们可以更改主线程的名称吗? 并在主要方法中
Thread t = Thread.currentThread();
System.out.println(t);
它打印:
Thread[main,5,main]
- 这里第一个线程名,第二个优先级,第三个是当前线程所属的线程组。
是吗?
线程组第三个参数是什么?
最佳答案
来自 Thread 的 Javadoc
public final void setName(String name)
Changes the name of this thread to be equal to the argument name.
和
public String toString()
Returns a string representation of this thread, including the thread's name, priority, and thread group.
Thread t = Thread.currentThread();
System.out.println(t);
t.setName("new thread name");
System.out.println(t);
打印
Thread[main,5,main]
Thread[new thread name,5,main]
要更改线程组的名称,您可以使用反射,但这不太可能是个好主意。
关于java - 主线程名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12600861/