线程的生命周期

本文详细介绍了Java线程的六种状态,包括新建、可运行、阻塞、无限等待、超时等待和终止状态,并阐述了状态之间的转换。线程在不同状态下,如等待资源、被调度执行、因锁阻塞等,都有不同的行为。理解这些状态对于多线程编程至关重要。
摘要由CSDN通过智能技术生成

线程状态源码:

  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;
    }

线程的六种状态:

  1. 新建状态: 线程对象创建了但是没有调用start()方法

  2. 可运行状态(Runnable)

    Running(运行中状态): 被调度器(scheduler)选中,CPU正在执行

    Ready(就绪状态): 等待调度器调度

    Ready ----> 调度器选中 ----> Running

    Running ----> 调度器切换 | Thread.yeild() 方法---> Ready

    3.无限等待状态(Waiting):
    1. Runnable --> wait()方法 | join() 方法 ----> Waiting

      Waiting -> notify()方法 | notifyAll() ----> Runnable

4. 超时等待状态(Timed_Waiting)

Runnable --> sleep(long millis) | wait(long millis) | join(long millis) ---> Timed_Waiting

Timed_Waiting --> 时间一到 | notify() | notifyAll() --> Runnable

5.Blocked(阻塞)

被监视器锁监视的线程,没有获取同步锁的被阻塞

Runnable ---> 没有获取锁 ----> 阻塞

阻塞 ---> 获取锁对象 ---> Runnable

6.Terminated(终止):

Runnable ----> 任务方法执行完毕|发生异常 ---> Terminated

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暖锋丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值