I have read one thing about threads
in the Java thread model API. It says that more than one thread can have the same name. But the PID of each thread will be unique, right? What is the name then? Is not the thread class name?
what is the name then? isnot the thread class name?
The name is whatever you set it to be using Thread.setName
. It's just for the purpose of diagnostics. If you don't call Thread.setName
or provide the name to the constructor, the value will be determined as per the docs:
Allocates a new Thread object. This constructor has the same effect as
Thread (null, null, gname)
, where gname is a newly generated name. Automatically generated names are of the form"Thread-"+n
, where n is an integer.
(It's obviously more useful diagnostically to give a specific thread name which indicates its purpose.)
When a new thread is created, it is assigned an automatically generated name which is of the form - "Thread-0"
, "Thread-1"
.
But, you can any time change the name of the Thread by using Thread#setName()
method: -
Thread.currentThread().setName("ABC");
Thread.currentThread()
returns the reference to current thread executing, and then you set the name of that thread to "ABC"
.
Nope it is a thread name, If you see a thread dump, you will know which thread is doing what exactly. :)
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