11.线程状态

从操作系统层面分为五种状态

1.初始状态

2.可运行状态

3.运行状态

4.阻塞状态

5.终止状态

从java API层面分为六种状态

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

Runnable状态(可运行态、运行态、阻塞态)

操作系统层面的阻塞状态(例如大文件长时间读取),在java API层面还是显示的Runnable状态(在idea中debug时候显示的是Running)。

java API层面的阻塞状态:blocked,waiting,timed_waiting

sleep方法对应timed_waiting

join方法对应waiting

synchronized加锁后,一个线程获取了锁,另一个线程获取不到该锁,对应blocked

1.调用线程start方法进入Runnable状态。java API层面的Runnable状态包含了(操作系统层面的阻塞io、正在运行的线程和准备运行的线程)。

2.调用线程wait方法进入waiting状态,进入了WaitSet集合中。调用notify,notifyAll,interrupt方法时从WaitSet集合中进入到EntryList集合中,如果竞锁成功,线程从waiting变成Runnable;如果竞锁失败,线程从waiting变成Blocked状态。(补充:idea工具的debug模式下,查看的线程状态为monitor对应了线程的blocked状态,Running对应了Runnable状态。)

3.当前线程调用t.join方法,当前线程从Runnable变成waiting。t线程运行结束时或者调用了当前线程的interrupt方法时,当前线程又从waiting变成Runnable。

4.当前线程调用了LockSupport.park方法会让当前线程从Runnable变成Waiting状态。调用了LockSupport.unpark(目标线程)或者调用了线程的interrupt方法时,会让目标线程从waiting变成Runnable。

5.wait(long timeout)线程从Runnable变成Time_waiting状态,如果是timeout时间到了,或者调用了notify,notifyAll,t.interrupt方法时,如果竞锁成功,线程从Time_waiting变成Runnable;如果竞锁失败,线程从Time_waiting变成Blocked状态。

6.t.join(long timeout)当前线程从Runnable变成Time_waiting状态,当前线程等待时间超过了timeout,或者调用了当前线程的interrupt方法时,或者t线程运行结束,当前线程从Time_waiting变成Runnable状态。

7.当前线程调用了Thread.sleep(long timeout)方法,当前线程从Runnable变成time_waiting状态,当前线程超过了timeout时间,当前线程从time_waiting变成Runnable状态。

8.当前线程调用LockSupport.parkNanos(long nanos)或者LockSupport.parkUntil(long millis)时,从Runnable变成time_waiting状态。反之调用了unpark,线程的interrupt或者等待超时,线程从time_waiting变成Runnable状态。

9.用synchronized(obj)获取对象锁竞争失败,从Runnable变成blocked状态。持obj锁线程的同步代码块执行完毕,会唤醒该对象上所有的blocked的线程重新竞争,其中t1线程竞争成功从Blocked变成Runnable,其他竞争失败的线程仍然是Blocked状态。

10.当前线程所有代码运行完毕,从Runnable变成Terminated状态。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卷土重来…

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

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

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

打赏作者

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

抵扣说明:

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

余额充值