java - 主线程名称

标签 java multithreading

我们可以更改主线程的名称吗? 并在主要方法中

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/

相关文章:

Java - 异常评估

java - 使用 JSON POST 外来字符会产生 400

java - 我不明白 Thread.sleep 和 'new' 运算符是如何工作的

multithreading - grails sessionFactory.currentSession.flushMode无法与线程一起使用?

java - 如何在我的 Ant 构建中包含 jar 库

java - 使用注释的 Struts 2 验证

java - 如何取消 JPanel 的重绘?

java - Android - 创建线程时为假

java - 使用线程在 GUI 中对形状进行动画处理

c - 奇怪的多线程行为