java 休眠线程处于什么状态?

jutyujz0  于 2023-02-02  发布在  Java
关注(0)|答案(2)|浏览(156)

我正在寻找以下方面的验证/论据:
线程在任何时间点都恰好位于5个(WAITING为+1--2)states中的一个。
假设线程T调用
线程休眠(3000);
从而使其自身进入睡眠3秒。
在这3秒钟内,它处于哪种状态?
它显然是start()-ed,并且仍然活着,因此既不是NEW,也不是TERMINATED
它没有等待任何锁,事实上,它还没有释放在这个调用之前的任何锁,所以它不是BLOCKED
它不是在等待另一个线程。在某种意义上是在等待自己--但这不是WAITING状态的规范。让线程进入WAITING状态的是以下调用之一:x一米八氮一x、x一米九氮一x和x一米十氮一x。
因此它应该是RUNNABLE--线程状态中唯一被遗漏的一个。然而,RUNNABLE是线程在JVM上执行的状态--为CPU时间而启用,因此为消耗资源而启用。处于CPU调度中的休眠线程似乎是矛盾的。
我错过什么了吗?
短暂性脑缺血

xwmevbvl

xwmevbvl1#

头状态.TIMED_WAITING的Javadoc为:
具有指定等待时间的等待线程的线程状态。由于使用指定的正等待时间调用以下方法之一,线程处于定时等待状态:

  • Thread.sleep
  • 带超时的Object.wait
  • 带超时的Thread.join
  • LockSupport.parkNanos
  • LockSupport.parkUntil

实际上,下面的测试程序打印TIMED_WAITING:

public class Test {
    public static void main(String[] args) throws Exception {
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        };
        t.start();
        Thread.sleep(100); // make sure the other thread has started
        System.out.println(t.getState());
    }
}
vlju58qv

vlju58qv2#

我的代码显示了TIMED_WAITING和RUNNABLE线程状态。下面是类www.example.com和www.example.com。在命令行使用javac Concurrent.java SimpleThread.java编译。 SimpleThread.java and Concurrent.java . Compile at the command line with, javac Concurrent.java SimpleThread.java. Run with, java Concurrent

网站SimpleThread.java

class SimpleThread extends Thread {
    
    private int id;
    
    // class constructor
    public SimpleThread(int id) {
        this.id = id;
    }
    
    // function to be executed by thread
    @Override
    public void run() {
        System.out.println("Thread " + id + " is running.");
        System.out.println("t" + id + " thread name: " + Thread.currentThread().getName());
        
        // interrupt the thread for 2 second (2000 ms)
        try {
            for (int i = 0; i < 5; i++) {
                System.out.println("t" + id + " tick - " + i);
                Thread.sleep(2000);
                System.out.println("t" + id + " " + this.getState());
            }
        } catch(InterruptedException e){
            System.out.println("Thread " + id + " is sleeping.");
        }
        
        System.out.println("Thread " + id + " execution is complete.");
    }
}

网站Concurrent.java

class Concurrent {
    public static void main(String[] args) {
    
        SimpleThread t1 = new SimpleThread(1);
        SimpleThread t2 = new SimpleThread(2);
        
        // thread state before it is started
        System.out.println("t1 thread state: " + t1.getState());
        System.out.println("t2 thread state: " + t2.getState());
        
        // execute thread run method
        t1.start();
        t2.start();
        
        // put t1 to sleep for 1 second
        try {
            t1.sleep(1000);
            System.out.println("t1 thread state " + t1.getState());
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        
        // finish all threads before returning to the main thread.
        try {
            t1.join();
            t2.join();      
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        System.out.println("t1 thread state: " + t1.getState());
        System.out.println("t2 thread state: " + t2.getState());
    }
}

程序输出

相关问题