面试题学习: 线程状态

目的

个人学习记录, 也是方便后续自己面试的复习.

资源

B站的一个讲高频面试题的一个学习视频

Java中线程状态

java.lang.Thread.State

    /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    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;
    }

Java线程状态的转换

通过Debug断点调试:

/**
 * 线程状态测试
 *
 * @author xiaozhengN 571457082@qq.com
 * @since 2022-10-18 07:33:11
 **/
public class TestThreadState {
    private static final Object LOCK = new Object();

    public static void main(String[] args) {
        testNewRunnableTerminated();
        testWaiting();
        testBlocked();
    }

    private static void testBlocked() {
        Thread t2 = new Thread(() -> {
            System.out.println("before sync"); // 3
            synchronized (LOCK) {
                System.out.println("in sync"); // 4
            }
        }, "T2");
        t2.start();
        System.out.println(t2.getState()); // 1
        synchronized (LOCK) {
            System.out.println(t2.getState()); // 2
        }
        System.out.println(t2.getState()); // 5
    }

    private static void testWaiting() {
        Thread t3 = new Thread(() -> {
            synchronized (LOCK) {
                System.out.println("before waiting"); // 1
                try {
                    LOCK.wait(); // 3
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T3");
        t3.start();
        System.out.println(t3.getState()); // 2
        synchronized (LOCK) {
            System.out.println(t3.getState()); // 4
            LOCK.notify(); // 5
            System.out.println(t3.getState()); // 6
        }
        System.out.println(t3.getState()); // 7
    }

    private static void testNewRunnableTerminated() {
        Thread t1 = new Thread(() -> {
            System.out.println("running..."); // 3
        }, "T1");
        System.out.println(t1.getState()); // 1
        t1.start();
        System.out.println(t1.getState()); // 2
        System.out.println(t1.getState()); // 4
    }
}

辨别两种说法: 6种状态 VS 5种状态

操纵系统层面有5种状态:
① 分到CPU时间的: 运行
② 可以分到CPU时间的: 就绪
③ 分不到CPU时间的: 阻塞
注意: JAVA中的 RUNNABLE 涵盖了 就绪, 运行, 阻塞I/O
在这里插入图片描述
JAVA层面:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值