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
线程执行结束处于该状态。