Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

thread name issue

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic