If multiple Java applications are running on a system, is each Thread ID unique relative to all other Java threads, regardless of what application they are running in?
J
Well, let me check the source.
In the Thread
's init
method (which is called by every constructor):
/* Set thread ID */
tid = nextThreadID();
In nextThreadID()
:
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
And:
/* For generating thread ID */
private static long threadSeqNumber;
It is never set, and thus defaults to 0.
So apparently thread ID numbers always start at 0 and increment by 1. In other words, the answer to your question is that they are not globally unique.
According to the JDK source, a thread ID is unique in a given JVM - in fact, it's simply implemented as a running sequence.
Here's the nextThreadID() method from 1.6.0_10:
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
(there's probably actually a long overflow bug in there, presumably it's never actually happened)