线程的几大状态

Javajava.lang.Thread类有一个变量threadStatus,标示了该线程的当前状态,它是一个int类型,但是对应的get方法返回值是一个枚举(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;
    }

New

Thread state for a thread which has not yet started.  

尚未启动的线程的线程状态。表明线程尚未开始

RUNNABLE
    可运行线程。一个线程在Java虚拟机处于可运行状态,即它在等待其他资源或操作系统的处理,一旦获取到CPU资源进行了执行,则进入人们所说的隐式RUNNING状态(这个状态JVM并不关心,我们也不是特别关注,一般的JVM监控工具都不会统计这种状态)。

  BLOCKED,

线程运行过程中,可能由于各种原因进入阻塞状态:       

出于某种原因,比如等待用户输入等而让出当前的CPU给其他的线程执行时,会进入BLOCKED状态。

线程通过调用sleep方法进入睡眠状态

所谓阻塞状态是正在运行的线程没有运行结束,暂时让出CPU,这时其他处于就绪状态的线程就可以获得CPU时间,进入运行状态。

阻塞状态的线程正在等待一个监视锁

进入一个同步块/方法或者在调用之后从新进入一个同步块/方法


WAITING,


A thread that is waiting indefinitely for another thread to perform a particular action is in this state. 
一个线程正在无限期地等待另一个线程来唤醒是在这种状态下。

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

Object.wait( with no timeout)

thread.join( with no timeout)

LockSupport.park( with no timeout)

一个线程处于WAITING状态需要由于另一个线程激活。例如,一个线程执行Object.wait()后会等待另一个线程调用对象Object.notify()或Object.notifyAll()

一个线程调用了另一个线程的Thread.join()方法,则在另一个线程执行后才会继续执行(join方法可以指定延迟执行时间)。


TIMED_WAITING

A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. 
一个线程在一个时间阈值内等待另一个线程来唤醒就处于这种状态,达到阈值则回到RUNNABLE状态

具有指定等待时间的等待线程的线程状态。一个线程在等待状态时,由于调用下列方法具有指定的正等待时间:

  1. 调用Thread.sleep()
  2. 调用Object.wait()超过时间阈值
  3. 调用Thread.join()超过时间阈值
  4. 调用LockSupport.parkNanos(Object,long)
  5. 调用LockSupport.parkUntil(Object,long)
TERMINATED

 

A thread that has exited is in this state. 
一个线程退出就处于这种状态。

线程执行完成就会变为这个状态。

程执行完了或者因异常退出了run()方法,该线程结束生命周期。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你是我的天晴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值