现成的状态分为如下五个:
NEW
RUNNABLE
RUNNING
BLOCKED
TERMINATED
NEW
新建对象 ,调用start 进入runnnable状态RUNNABLE
调用start才会进入此状态,只有获得cpu的时间片才会执行 此状态不会直接进入blocked状态,必须获得cpu的调度执行权才可以, 严格讲,只能意外终止或者进入running状态RUNNING
RUNNING状态的线程一定是RUNNABLE,RUNNABLE状态不一定是RUNNABLE线程状态转换
1. 直接进入**TERMINATED**状态,调用stop方法或者判断状态终止 2. 进入BLOCKED 2.1 调用sleep(不释放锁),wait进入waitset队列 2.2 IO阻塞,读写网络数据 2.3 获取某个锁资源,加入到该锁的阻塞队列 3. RUNNABLE 3.1 CPU调度 3.2 调用yield释放cpu使用权 **BLOCKED** 1.直接进入**TERMINATED**状态,调用stop方法JVM crash 2.线程阻塞结束,进入runnble状态 3. 线程获得锁,进入runnable状态 。 4.waitset中的线程,调用notify/notify all 进入runnable状态 5.线程在阻塞过程中,其他线程调用了interupt方法,进入RUNNABLE状态 **TERMINATED** 最终状态: 1. 运行结束 2. 抛出异常 3. JVM crash在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; }NEW
线程创建的状态,还未启动RUNNABLE
线程可运行状态,获取CPU时间片可以执行BLOCKED
线程等待获取锁监视器,处于阻塞状态的线程, 等待进入同步代码块或者方法,或者调用了 object.wait状态重新等待进去同步代码块或者方法WAITING
处于此状态的线程因为调用了以下方法: Object.wait 无超时等待 Thread.join 无超时等待 LockSupport.park 无超时park 处于当前状态的线程等待其他线程做以下操作 调用Thread.wait状态的线程等待 其他线程调用 Object.notify或Object.notifyAll 调用Thread.join方法的线程等待另一个线程执行结束。 调用LockSupport.pak的线程等待其他线程调用LockSupport.unparkTIME_WAITING
等待指定时间 当调用以下方法处于TIME_WAITING状态 Thread.sleep Thread.join(1) Thread.wait(1) LockSupport.parknanos LockSupport.parkUntilTERMINATED
线程执行结束或者JVM crash 或者线程执行异常