程序员

线程的状态

2023-11-29  本文已影响0人  我可能是个假开发

一、操作系统层面的五种状态

image.png

二、 Java API 层面的六种状态

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;
    }
@Slf4j
public class StateTest {
    public static void main(String[] args) throws IOException {
        // new
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                log.debug("running...");
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                while(true) {

                }
            }
        };
        // runnable
        t2.start();

        Thread t3 = new Thread("t3") {
            @Override
            public void run() {
                log.debug("running...");
            }
        };
        // terminated
        t3.start();

        Thread t4 = new Thread("t4") {
            @Override
            public void run() {
                synchronized (StateTest.class) {
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        // timed_waiting
        t4.start();

        Thread t5 = new Thread("t5") {
            @Override
            public void run() {
                try {
                    t2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        // waiting
        t5.start();

        Thread t6 = new Thread("t6") {
            @Override
            public void run() {
                synchronized (StateTest.class) {
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        // blocked 拿不到锁
        t6.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("t1 state {}", t1.getState());
        log.debug("t2 state {}", t2.getState());
        log.debug("t3 state {}", t3.getState());
        log.debug("t4 state {}", t4.getState());
        log.debug("t5 state {}", t5.getState());
        log.debug("t6 state {}", t6.getState());
        System.in.read();
    }
}
15:16:58.719 [t3] DEBUG juc.thread.StateTest - running...
15:16:59.230 [main] DEBUG juc.thread.StateTest - t1 state NEW
15:16:59.238 [main] DEBUG juc.thread.StateTest - t2 state RUNNABLE
15:16:59.238 [main] DEBUG juc.thread.StateTest - t3 state TERMINATED
15:16:59.239 [main] DEBUG juc.thread.StateTest - t4 state TIMED_WAITING
15:16:59.239 [main] DEBUG juc.thread.StateTest - t5 state WAITING
15:16:59.239 [main] DEBUG juc.thread.StateTest - t6 state BLOCKED

三、线程状态转换

image.png

1.NEW --> RUNNABLE

当调用 t.start()方法时,由 NEW --> RUNNABLE

2.RUNNABLE <--> WAITING

t 线程用 synchronized(obj) 获取了对象锁后:

3.RUNNABLE <--> WAITING

4.RUNNABLE <--> WAITING

5.RUNNABLE <--> TIMED_WAITING

t 线程用 synchronized(obj) 获取了对象锁后:

6.RUNNABLE <--> TIMED_WAITING

7.RUNNABLE <--> TIMED_WAITING

8.RUNNABLE <--> TIMED_WAITING

9.RUNNABLE <--> BLOCKED

10.RUNNABLE <--> TERMINATED

当前线程所有代码运行完毕,进入 TERMINATED

上一篇下一篇

猜你喜欢

热点阅读