当前位置:网站首页>6 states of a thread

6 states of a thread

2022-08-09 06:27:00 The koala over rice

线程的6种状态

NEW 状态是指线程刚创建, 尚未启动

RUNNABLE 状态是线程正在正常运行中, 当然可能会有某种耗时计算/IO等待的操作/CPU时间片切换等, 这个状态下发生的等待一般是其他系统资源, 而不是锁, Sleep等

BLOCKED 这个状态下, 是在多个线程有同步操作的场景, 比如正在等待另一个线程的synchronized 块的执行释放, 或者可重入的 synchronized块里别人调用wait() 方法, 也就是这里是线程在等待进入临界区

WAITING 这个状态下是指线程拥有了某个锁之后, 调用了他的wait方法, 等待其他线程/锁拥有者调用 notify / notifyAll 一遍该线程可以继续下一步操作, 这里要区分 BLOCKED 和 WATING 的区别, 一个是在临界点外面等待进入, 一个是在理解点里面wait等待别人notify, 线程调用了join方法 join了另外的线程的时候, 也会进入WAITING状态, 等待被他join的线程执行结束

TIMED_WAITING 这个状态就是有限的(时间限制)的WAITING, 一般出现在调用wait(long), join(long)等情况下, 另外一个线程sleep后, 也会进入TIMED_WAITING状态

TERMINATED 这个状态下表示 该线程的run方法已经执行完毕了, 基本上就等于死亡了(当时如果线程被持久持有, 可能不会被回收)

java的Thread类中定义了一个Stateenum inner class,for the above6Each type corresponds to an attribute in the enumeration

// 取自jdk8 Thread源码
public enum State {
    
        /** * Thread state for a thread which has not yet started. */
        NEW,

        /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */
        RUNNABLE,

        /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */
        BLOCKED,

        /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */
        WAITING,

        /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */
        TIMED_WAITING,

        /** * Thread state for a terminated thread. * The thread has completed execution. */
        TERMINATED;
    }

An example is given6types of appearance

public class TestState {
    
    public static void main(String[] args) {
    
        Thread t1 = new Thread("t1") {
    
            @Override
            public void run() {
    
                log.debug("t1 running...");
            }
        };
        Thread t2 = new Thread("t2") {
    
            @Override
            public void run() {
    
                while (true) {
    

                }
            }
        };
        t2.start();
        Thread t3 = new Thread("t3") {
    
            @Override
            public void run() {
    
                synchronized (TestState.class) {
    
                    try {
    
                        sleep(30000000L);
                    } catch (InterruptedException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        };
        t3.start();
        Thread t4 = new Thread("t4") {
    
            @Override
            public void run() {
    
                log.debug("t4 running...");
            }
        };
        t4.start();
        Thread t5 = new Thread("t5") {
    
            @Override
            public void run() {
    
                try {
    
                    t2.join();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
        };
        t5.start();
        Thread t6 = new Thread("t6") {
    
            @Override
            public void run() {
    
                synchronized (TestState.class) {
    
                    try {
    
                        sleep(30000000L);
                    } catch (InterruptedException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        };
        t6.start();
        try {
    
            Thread.sleep(500);
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        }
        System.out.println("t1:" + t1.getState()); // t1:NEW
        System.out.println("t2:" + t2.getState()); // t2:RUNNABLE
        System.out.println("t3:" + t3.getState()); // t3:TIMED_WAITING
        System.out.println("t4:" + t4.getState()); // t4:TERMINATED
        System.out.println("t5:" + t5.getState()); // t5:WAITING
        System.out.println("t6:" + t6.getState()); // t6:BLOCKED
    }
}

Thread switching flowchart
在这里插入图片描述

原网站

版权声明
本文为[The koala over rice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090622258967.html