Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threads having the same name

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?

like image 786
doniyor Avatar asked Nov 25 '12 20:11

doniyor


3 Answers

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.)

like image 86
Jon Skeet Avatar answered Oct 10 '22 22:10

Jon Skeet


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".

like image 30
Rohit Jain Avatar answered Oct 10 '22 23:10

Rohit Jain


Nope it is a thread name, If you see a thread dump, you will know which thread is doing what exactly. :)

like image 1
damiankolasa Avatar answered Oct 10 '22 22:10

damiankolasa