Is it possible to retrieve the start time of a given java thread within the JVM?
I have a thread dump and am looking at some problematic threads which I would like to correlate to a specific operations in the application log using time.
No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.
start() method causes this thread to begin execution, the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.
There is no method in the Java API that provides you this information. Besides, it may not be up useful anyways. Consider the case of a thread pool where thread creation is not necessarily tied to application-level events.
If you are in full control of thread creation, then you may attach a thread-local variable to the thread that records its creation time.
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