• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread name issue

 
liiii zhao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());

t.start();

t2.start();

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

}
************
output:
Thread-1
Thread-1
Thread-3
Thread-3
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Edit:
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
andy wen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in Thread constructor

and

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
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Henry
 
Paul Clapham
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic