7

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?

user1452076
  • 127
  • 12

3 Answers3

1

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!

Brandon
  • 9,822
  • 3
  • 27
  • 37
1

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

codeghost
  • 1,014
  • 7
  • 14
0

If you have the ability to define your own Thread subclass, you could do something like this:

class WugThread extends Thread
{
    // constructors

    long timeStarted = -1;

    public void start()
    {
        super.start();
        timeStarted = System.currentTimeMillis();
    }
}
Wug
  • 12,956
  • 4
  • 34
  • 54
  • Obviously, it won't necessarily reflect EXACTLY when the thread started, but it shouldn't differ by more than a few milliseconds from when the thread actually begins to execute, and is by far the easiest way to track this information. – Wug Mar 18 '13 at 14:43
  • 2
    The OP said he can't do this. – Brandon Mar 18 '13 at 14:45
  • 1
    I'll leave the answer there for the reference of people who find the question in the future. Short of doing something really nasty involving replacing the system implementation of thread with one that supported this, you're probably in some trouble. getThreadCpuTime will return the total time the thread spends working on a CPU, including kernel time, and getThreadUserTime will return the total time the thread has spent in userland on the CPU. You're right, time spent sleeping won't show up here. – Wug Mar 18 '13 at 14:53