Solved

Java thread naming convention

Posted on 2015-01-07
2
435 Views
Last Modified: 2015-01-23
Hi,
I have a java code. In which i print the thread name using : Thread.currentThread().getName()
At one place i get the output = pool-15-thread-1 and at other = Thread-5028

The thread name convention seems to be different for both. What does the output signify ?
Why one name is having pool word and what are the significance of the numbers 15, 5028 ?

Thanks
0
Comment
Question by:Rohit Bajaj
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40536733
Well, that's because there is no convention. The application (or the libraries/utilities that it uses) is responsible for naming Threads that it creates. However, due to the names that you have I can guess that the first was probably a thread that is part of a thread pool created with java.util.concurrent.Executors utility class either directly or indirectly, ie. the DefaultThreadFactory that is part of the Executors class is exposed publically, so any library code can use it and threads would be named with the same convention pool-X-thread-Y, and from a quick look things like Google's guava and Java's own Swing libraries do use it.

The second thread looks like it was created with a simple "new Thread(runnable);" sort of call, as when you create a Thread in this fashion, ie. without explicitly specifying a name, a name of Thread-XXX is given to it for you.

I guess the point to all the above, is that you (or the libraries that you use) can give more meaningful names to the threads if you which. In the case of pools create via the Executors class, you can define your own ThreadFactory that creates and names threads in an application specific way. Also, if you manually create Thread objects using the new operator, you can call the constructors that pass a String as the name (or you can call setName() after the Thread is created).
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40536743
Ah, sorry, should have also added that if indeed those two threads have been created in the way that I have assumed, then the numbers are just auto-increment fields, ie. pool-15-thread-1 is from the 15th pool that has been created by an Executors defaultThreadFactory and it's the first thread created in that pool, similarly Thread-5028 is the 5028th thread created by a call to "new Thread()" where no name has been explicitly given, ie. anonymous thread.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 68
topping1 challenge 7 50
wordlen challenge 3 46
Python Assistance 7 31
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now