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'.
The name for a new thread can be set via the “name” argument in the constructor to the threading. Thread class when creating a new thread.
In C#, a user is allowed to assign a name to the thread and also find the name of the current working thread by using the Thread.Name property of the Thread class. Here, the string contains the name of the thread or null if no name was assigned or set.
Gets or sets the culture for the current thread. Gets or sets the thread's current principal (for role-based security). Gets the currently running thread. Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run-time.
The run() method of thread class is called if the thread was constructed using a separate Runnable object otherwise this method does nothing and returns. When the run() method calls, the code specified in the run() method is executed. You can call the run() method multiple times.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With