如何知道线程处于 "waiting" 的状态?

How to know the condition at which a thread is "waiting" for?

我正在调试一个实时环境,我已经进行了一些线程转储。我看到很多线程是 "waiting on condition";但是我怎么知道那个条件是什么?

下面是真实线程转储堆栈的片段:

"Uploader-pool-job-create-ca2d264a-51b3-4fa3-8113- 
f0f0aca47add-StreamThread-10-0_14" #140 daemon prio=5 os_prio=0 
tid=0x00007f006002b800 nid=0x18b waiting on condition 
[0x00007f004d9e1000]
 java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000002c20d4320> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

其他:

"Uploader-pool-job-create-ca2d264a-51b3-4fa3-8113- 
f0f0aca47add-StreamThread-3-0_16" #138 daemon prio=5 os_prio=0 
tid=0x00000000025da000 nid=0x189 waiting on condition 
[0x00007f004dbe3000]
java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000002c20d6690> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

在这些线程中它说 "waiting on condition";但我无法破译线程等待的条件是什么?

--> 一般来说,有没有关于如何理解线程转储的指南。

在我看来他们正在等待获取锁。这种情况下的条件是:等待超时完成以尝试获取锁。

java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000002c20d4320> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)

一切都在堆栈跟踪中 - ScheduledThreadPoolExecutorawaiting on the available 条件:

private final Condition available;
. . .

     available.awaitNanos(delay);   // ScheduledThreadPoolExecutor.java:1093

换句话说,池中的线程处于空闲状态,正在等待更多工作。

一般情况下,堆栈跟踪会准确显示Java文件名和当前执行线程所在的行号(通常在线程等待时,最深的几层也会low-level,所以继续沿着链向上寻找最有意义的水平)。