I have a Java web application (1.8) running in a Tomcat. The application logs to a log file, including the thread-ID: 06/30 11:30:51 (101970) [userInfo Jun 30 11:30:51 CEST 2022] INFO some.class.name [] - Some Log Message
I recently noticed that the Thread-Id has become very large (1600000 ) and wonder if that indicates a problem with the Threadpooling (i.e. threads not being returned to the pool).
According to the Thread class, the ID should be a unique number that is increasing (and might be reused). I assumed that this means that the ID should increase only if a new additional Thread had to be created.
However, I am currently monitoring the Thread counts (via ThreadMXBean) and see that the thread-id-number is much higher than the Peak Thread Count.
Does anyone have insights of how the thread-id is created and if there is any correlation between a high thread id and the number of threads?
CodePudding user response:
In java.lang.Thread
we see the following very simple little piece of code:
/* For generating thread ID */
private static long threadSeqNumber;
private static synchronized long nextThreadID() {
return threadSeqNumber;
}
What this means is that every time a new thread is created, a new id is issued, regardless of whether previously created threads have terminated or not. What this means is that a thread id will never be reused, and I would speculate that thread id is long
for the sole purpose of guaranteeing that you will never run out of thread ids.
Now, thread-pools as well as other functionalities of the JVM create and destroy threads all the time. For example, if a thread pool has not had any use for one of its threads for some time, it may destroy that thread, and re-create it later, when it discovers that it has a use for it.
This means that as your software keeps running for hours, you will inevitably be seeing higher and higher thread ids. That's normal.