Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread start time

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.

like image 690
bob dabelina Avatar asked Mar 09 '15 04:03

bob dabelina


People also ask

Can we start thread 2 times?

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.

What is start in Java thread?

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).

What is the start () and run () method of thread class?

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.


1 Answers

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.

like image 177
amahfouz Avatar answered Nov 01 '22 02:11

amahfouz