线程转储始终显示线程状态TIMED_WAITING

时间:2019-07-07 18:41:08

标签: java multithreading threadpoolexecutor jstack

我创建了一个使用Executor框架的程序。我创建了一个定期调度的线程。

以下是相同的完整源代码:

package com.example;
import java.security.SecureRandom;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExecutorFrameworkDemo {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorFrameworkDemo.class);

    static {
        long refresh = 120000;
        long initialDelay = 120000;
        ScheduledExecutorService scheduledThreadPool = null;
        SecureRandom sr = new SecureRandom();
        scheduledThreadPool = Executors.newSingleThreadScheduledExecutor((Runnable run) -> {
        Thread t = Executors.defaultThreadFactory().newThread(run);
        t.setDaemon(true);
        t.setName("Demo-pool");
        t.setUncaughtExceptionHandler(
                (thread, e) -> LOGGER.error("Uncaught exception for Demo-pool thread " + thread.getName(), e));
        return t;
    });

    scheduledThreadPool.scheduleAtFixedRate(() -> {
        System.out.println("Executing thread " + Thread.currentThread().toString() + "at" + new Date());

    }, initialDelay + sr.nextInt((int) refresh / 4), refresh + sr.nextInt((int) refresh / 4),
            TimeUnit.MILLISECONDS);
    }
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Inside main thread");
        Thread.sleep(50000000);
        System.out.println("Inside main thread, after main's Sleep delay");
    }
}

它创建一个在固定的Schedule中运行的单线程;实际上,每隔2分钟+几秒钟,我就会看到输出。

同时,我连续进行线程转储,希望在某个时间点线程状态为RUNNABLE,但是它总是给我TIMED_WAITED

以下是相同的实际线程转储:


"Demo-pool" #12 daemon prio=5 os_prio=31 tid=0x00007f96f3230000 nid=0x5603 waiting on condition [0x0000700007752000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000007976182f0> (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:1074)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748) 

我不能看到RUNNABLE状态吗?

我正在使用jstack和shell脚本在无限循环中进行线程转储,如下所示:

进行线程转储的脚本:

#!/bin/bash
itr=0
while true
  do
     (( ++itr ))
      jstack $1 > jstack_Iteration_${itr}
  done

其中,$ 1是java进程的PID,作为命令参数传递。

1 个答案:

答案 0 :(得分:1)

要在RUNNABLE中看到一个线程,则意味着您在转储执行System.out.println的确切微秒内进行了转储。那是不可能的。对Runnable进行更耗时的实施,例如循环形式的繁忙等待:

for (int i = 0; i < 10000000; i++) {
    // do nothing
}

请勿执行Thread.sleepwait,否则您将处于RUNNABLE之外的另一状态。