简体   繁体   English

Java线程开始时间

[英]Java thread start time

Is it possible to retrieve the start time of a given java thread within the JVM? 是否可以在JVM中检索给定java线程的开始时间?

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. Java API中没有为您提供此信息的方法。 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. 是的,如果您在Linux上运行Oracle JDK / OpenJDK,则可以。

The idea is to find native thread id (TID) and then look at the modification time of /proc/JAVA_PID/task/TID pseudo file. 想法是找到本机线程ID(TID),然后查看/proc/JAVA_PID/task/TID伪文件的修改时间。

If you have a Thread Dump, then this is trivial: the native thread id would be printed by the thread header. 如果你有一个线程转储,那么这是微不足道的:本机线程id将由线程头打印。

For example, Java process PID is 2086. You type 例如,Java进程PID是2086.您键入

$ 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. 其中nid=0x86a是本机线程ID。 0x86a = 2154, so you'd like to explore task 2154 of the process 2086: 0x86a = 2154,所以你想要探索过程2086的任务2154:

$ 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”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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