adamM adamM - 1 year ago 98
Java Question

java: thread ID reuse of ID

Acording to the docs of


Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused. [emphasis added]

However, in what cases will a thread ID be reused? Looking at the source code for
, the method to get the next thread ID just increments a static long:

private static synchronized long nextThreadID() {
return ++threadSeqNumber;

I don't see how this code will re-use an old ID, unless by re-use they simply mean that eventually an incremented long will loop back to a previous value (but that does not make sense because the long will become negative before doing so - which breaks the docs).

Question in a nutshell: in what way are Thread IDs reused, and what causes them to be reused?

Answer Source

JDK classes can have many different implementations. You looked at an implementation of Thread, and found that it would never reuse thread-IDs; but the Javadoc is merely saying that a thread's ID may be reused after it's terminated, that is, that a JDK implementation may reuse the thread-ID of a terminated thread.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download