Thread foo = new Thread("Foo");
... 或通过调用Thread#setName
来设置线程名称:
public final void setName (String threadName)thread.setName("Thread-11");
或类似Thread.currentThread().setName("Thread-11");
。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);