Is there a way to set a friendly name to a thread in code?
For example, I want the thread with name Thread-11 on the image was named something like 'MyImportThread'.
Is there a way to set a friendly name to a thread in code?
For example, I want the thread with name Thread-11 on the image was named something like 'MyImportThread'.
您可以轻松地在 Its Constructor中传递线程名称,例如:
Thread foo = new Thread("Foo");
...或通过调用Thread#setName
:
public final void setName (String threadName)
设置线程的名称。
作为thread.setName("Thread-11");
或喜欢Thread.currentThread().setName("Thread-11");
检查Thread
构造函数,有几个带String name
参数的。或者您可以调用setName(String)
现有线程。
类Thread有一个方法:
public final void setName (String threadName)
Since: API Level 1
Sets the name of the Thread.
你试过了吗?
尝试这个:
Thread thread = new Thread("MyImportThread") {
public void run(){
// code
}
};
thread.start();
System.out.println(thread.getName());
是的,您可以使用以下方法为线程设置名称:
Thread.getCurrentThread().setName(threadName);