Solved

Java thread naming convention

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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