File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes thread name issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread name issue" Watch "thread name issue" New topic

thread name issue

liiii zhao

Joined: Aug 26, 2012
Posts: 1
i run the following code in my eclipse, and i excpt the thread name will come up like Thread-1 , Thread-2 & Thread-3 randomly. But the fact is the Thread-2 never comes up no matter i try, do you know why
public class Fabric extends Thread {

public static void main(String[] args) {
Thread t = new Thread(new Fabric());
Thread t2 = new Thread(new Fabric());



public void run() {
for (int i = 0; i< 2; i++) {
System.out.println (Thread.currentThread().getName()+" ");

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

There are more threads in any given Java application than you control. There is a thread for garbage collection, for example, as well as other ones for control and communications with the IDE and debuggers. You have no control over these threads, and you have no control over the names of threads in the default thread group - so you shouldn't rely or expect anything regarding those thread names. The fact that you get Thread-1 and Thread-3 is a product of your environment, and would be different if running in a different IDE or a different runtime or OS. If you really need to rely and control thread names you should create a ThreadGroup to create your Threads in, so their names are in your control, and not influenced by threads outside your group.

Actually, the ThreadGroup isn't enought to get reliable names, but it is enough to get a list of your threads without interference. To get control of the Thread names you need to use the Thread constructors which provide a name for the Thread. Perhaps the best way to repeatedly generate these threads would be to implement a ThreadFactory...

Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
To avoid confusion better to have name for a thread.

Thread t = new Thread(new Fabric(),"Thread-1");
Thread t2 = new Thread(new Fabric(),"Thread-2");

SCJP 1.5
andy wen

Joined: Aug 26, 2012
Posts: 1
Look in Thread constructor


So if you using

you may probably get Thread-0 and Thread-1 (first is 0)

But you have call Thread constructor twice.
new Fabric() will call Thread constructor once,
new Thread(...) will call Thread constructor again.
So you can just get Thread-1 and Thread-3

And as above reply, if you rely on the Thread name, good choice is giving them a name by yourself.
Henry Wong

Joined: Sep 28, 2004
Posts: 20514

As already mentioned, these thread names are created per thread object (whose name is not specified), and not per thread started.... so, in the original example, you would get the Thread-0 and Thread-2 threads if they were started using the other two thread objects that were instantiated.

Also, these names are provided as a means to give a name to all thread objects. It is a generic name provided when the application don't care about the name -- if the application cared, it would have specified a name. So... as already mentioned, if you care about the name, then specify it. If you don't care about the name, then what does it matter if the numbering systems skips or not.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

For all you know, a thread with the name of "Thread-2" might have started and finished before you got around to looking at the list of active threads.

But really, as others have already said, it doesn't matter what the JVM calls its threads and you shouldn't spend any time worrying about it.
I agree. Here's the link:
subject: thread name issue
It's not a secret anymore!