Java线程状态及切换

java线程状态类java.lang.Thread.State定义如下:

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线程的状态类共有6中状态,具体含义如下:

1、新建状态(NEW)

在Thread创建之后,start方法调用之前线程是新建状态。

2、运行状态(RUNNABLE)

这个状态包含两个部分:

a 、就绪状态,Thread调用start方法之后线程就进入就绪状态,这个状态下线程是随时可以运行,但具体啥时候执行取决于JVM线程调度器的调度。

b、执行状态,当线程获得CPU的资源开始执行run方法

3、阻塞状态(BLOCKED)

阻塞状态是线程在等待监视器锁,比如调用synchronized方法或者代码块,该锁被其他线程占用是会进入该状态,当线程获取到锁时,则进入运行状态等待CPU调度

4、等待状态(WAITING)

等待线程的状态,进入该状态的线程需要被其他线程唤醒才能重新进入运行状态

调用以下方法之一,线程处于等待状态:

Object.wait()方法,调用notify()随机唤醒一个等待状态线程,调用notifyAll()唤醒所有等待线程
Thread.join()方法,等待join的线程执行完毕,进入运行状态
LockSupport.park()方法,调用LockSupport.unpark()

5、有时限等待状态(TIMED_WAITING)

线程等待特定的时间,之后进入运行状态

调用以下方法之一,线程处于超时等待状态:

Object.wait(long)方法

Thread.sleep(long)方法

Thread.join(long)方法
LockSupport.park()方法

6、结束状态(TERMINATED )

线程已经执行完毕,或者异常终止。

最后说明一下线程在操作系统层面的5种状态:

1、初始状态

仅在语言层面创建了线程,还未与操作系统关联。

2、可运行状态

该线程已经进入就绪状态(已经与操作系统关联),可以被CPU调度。

3、运行状态

线程已经获取了CPU,开始执行线程任务,当CPU片段用完之后,线程会在可运行和运行状态之间切换。

4、阻塞状态

调用阻塞API,线程不再参与CPU调度,只等被系统重新唤醒,进入可运行状态。

5、终止状态

线程执行完毕,生命周期已经结束,不能再转变成其他状态。

参考文章:

java 多线程的6种状态

Java 并发 -- 线程状态(Java线程六种状态、操作系统层次的五种状态)、线程池(七大核心参数)、wait vs sleep

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值