java线程的几种状态转化图:
Java线程一共有七个状态,分别是新建(New),就绪(Ready to run),运行中(Running),睡眠(Sleeping),阻塞(Blocked),等待(Waiting),死亡(Dead/Terminate)
线程间的状态转化
1、新建(new)
新建一个线程,还未调用start方法
Thread t = new Thread();
2、就绪(runnable)
线程对象创建后,其他线程调用了该对象的start方法后,该状态的线程位于可运行线程池中,等待被线程调度选中,获取cpu的使用权
3、运行中(running)
可运行状态(Runnable)线程获得了cpu时间片(timeslice),执行程序代码
4、睡眠(sleeping)
也称TIME_WAITING(有等待时间的等待状态)
线程主动调用这几个方法:
1)Thread.sleep方法
2)object的wait方法 带有时间
3)Thread.join方法,带有时间
4)LockSupport的parkNanos方法,带有时间
5、阻塞(blocked)
阻塞状态是线程因为某种原因放弃了cpu使用权,暂时停止运行。直到线程进入可运行状态,才有机会再次获得cpu timeslice转到运行状态,阻塞的情况有两种:
1)同步阻塞:运行的线程进入了一个sychronized方法,若该同步锁被别的线程占用,则jvm会把该线程放入锁池中
2)其他阻塞:运行的线程发出了IO请求时,JVM会把该线程置为阻塞状态,当IO处理完毕后,线程重新转入可运行状态
6、等待(waiting)
运行中的线程执行了4个方法中的任意方法:
1)Object的wait方法,并没有设置时间
2)Thread的join方法,并没有设置时间
3)Locksupport的park方法
4)Condition的wait方法
7、死亡(dead)
线程run方法、main方法执行结束,或者因为异常退出了run方法,则该线程结束生命周期。死亡线程不可复生。
/** * A thread state. A thread can be in one of the following states: *
- *
- { @link #NEW} * A thread that has not yet started is in this state. * *
- { @link #RUNNABLE} * A thread executing in the Java virtual machine is in this state. * *
- { @link #BLOCKED} * A thread that is blocked waiting for a monitor lock * is in this state. * *
- { @link #WAITING} * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * *
- { @link #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. * *
- { @link #TERMINATED} * A thread that has exited is in this state. * *
* A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since 1.5 * @see #getState */ 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: *- *
- { @link Object#wait() Object.wait} with no timeout *
- { @link #join() Thread.join} with no timeout *
- { @link LockSupport#park() LockSupport.park} *
A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * 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: *
- *
- { @link #sleep Thread.sleep} *
- { @link Object#wait(long) Object.wait} with timeout *
- { @link #join(long) Thread.join} with timeout *
- { @link LockSupport#parkNanos LockSupport.parkNanos} *
- { @link LockSupport#parkUntil LockSupport.parkUntil} *