I've got a request to create an analysis of running threads within a JVM to monitor for long running jobs. Is there any way to find the starting date/time of a Java thread? I have no problem getting the threads, but I cannot figure out any way to to find out how long the thread has been active or when it started. To get the threads, I am simply enumerating over the ThreadGroup.
Note that I have no control over the actual threads themselves, so I can't put in any time or property and log the start time myself. All I have it the actual thread itself and need to determine the data from that. I can find two methods on the thread -- "getThreadCpuTime()" and "getThreadUserTime()" but I'm not sure those are enough, since apparently the thread will occasionally invoke a sleep() method, and I'm afraid that the "sleep" time would not be included in either of these methods.
Is there any way to determine the start time for a thread? Or will either of the two time methods return how long a thread has been active?
Could this be an X-Y problem?
http://www.perlmonks.org/index.pl?node_id=430320
I do not know of any way to determine when a thread started. But I am not sure that is really relevant information. Are you trying to profile performance? Are you seeking to use long-running threads as an indicator of poor performance?
The problem with that is that threads get re-used all the time. Tomcat pools its AJP and HTTP threads. When they receive a new request, a thread will break out of its loop and perform some action, then return to a waiting state. You want to measure that action, not the thread in its entirety.
Take another example, the garbage collector thread. That will always be a long-running thread because it starts at JVM start up time!
Whilst you may not be able to extend Thread, you could look into using Aspect Oriented Programming to intercept a new Runnable starting up and log the start time at that point.
Take a look at AspectJ / Spring AOP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With