Solved

Java thread naming convention

Posted on 2015-01-07
2
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 36

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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

615 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