线程的状态

    Thread源码里定义了6种状态,如下:


     * 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>

     1 NEW:
     一个线程被创建后,还没有调用start()方法,线程处于该状态。

     2 RUNNABLE:
     这个状态又分为2个小状态:READY和RUNNING,READY:处于NEW状态的线程,调用start()后,还没被cpu执行处于该状态。当被cpu执行时,就变为RUNNING状态了。一个RUNNING线程调用yield()会进入READY状态,在READY状态被CPU执行时进入了RUNNING状态。

     3 BLOCKED
     当线程等待锁,像synchronied锁和ReentrantLcok锁等,会处于这种状态。

     4 WAITING
     当线程等待其他线程的时候会处于这个状态,像RUNNING状态的线程调用Object.wait()或者LockSupport.park()或者otherThread.join()方法。和BLOCKED区别是BLOCKED等待的是锁,WAITING等待的是线程。

     5 TIMED_WAITING
     他是一种等待时间已知的WAITING状态,在方法调用时候会带时间参数。

     6 TERMINATED
     线程执行结束处于该状态。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值