Solved

Java thread naming convention

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

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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