简体   繁体   中英

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.

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.

Yes, it is possible if you are running Oracle JDK / OpenJDK on Linux.

The idea is to find native thread id (TID) and then look at the modification time of /proc/JAVA_PID/task/TID pseudo file.

If you have a Thread Dump, then this is trivial: the native thread id would be printed by the thread header.

For example, Java process PID is 2086. You type

$ jstack 2086

and get a stack trace with the following thread of interest:

"Thread-26" prio=10 tid=0x00007f96c80c2800 nid=0x86a waiting on condition [0x00007f96c0ff2000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)

Where nid=0x86a is the native thread ID. 0x86a = 2154, so you'd like to explore task 2154 of the process 2086:

$ ls -ld /proc/2086/task/2154
dr-xr-xr-x 6 user user 0 Mar 10 23:12 /proc/2086/task/2154
                         ^^^^^^^^^^^^
                         the thread start time

尝试使用“ps -p $ pid -wLo lwp,lstart”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM