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'.
You can easily pass a thread name in Its Constructor, like:
Thread foo = new Thread("Foo");
... or by calling Thread#setName
:
public final void setName (String threadName)
Sets the name of the Thread.
as thread.setName("Thread-11");
or like Thread.currentThread().setName("Thread-11");
Check the Thread
constructors, there are a few with a String name
parameter. Or you can call setName(String)
on an existing Thread.
Did you try something like this?
Thread.currentThread().setName("MyThread");
Have look also at Threads reference
especially at constructors.
The class Thread has a method for that:
public final void setName (String threadName)
Since: API Level 1
Sets the name of the Thread.
Did you try it?
Try this:
Thread thread = new Thread("MyImportThread") {
public void run(){
// code
}
};
thread.start();
System.out.println(thread.getName());