Solved

Java thread naming convention

Posted on 2015-01-07
2
481 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

16 Experts available now in Live!

Get 1:1 Help Now