首先看JDK中的代码: java.lang.Thread.Statejava
/** * 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> * * <p> * 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: * <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; }
新建立了一个线程对象,还未调用start()方法。this
线程对象建立后,其余线程(好比main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中 获取cpu 的使用权 。线程
可运行状态(runnable)的线程得到了cpu 时间片(timeslice) ,执行程序代码。code
也能够称做 TIMED_WAITING(有等待时间的等待状态)。orm
线程主动调用如下方法:对象
Thread.sleep方法;生命周期
Object的wait方法,带有时间;ci
Thread.join方法,带有时间;get
LockSupport的parkNanos方法,带有时间。同步
运行中(Running)的线程执行了如下方法:
Object的wait方法,而且没有使用timeout参数;
Thread的join方法,没有使用timeout参数;
LockSupport的park方法;
Conditon的await方法。
阻塞状态是指线程由于某种缘由放弃了cpu 使用权,暂时中止运行。直到线程进入可运行(runnable)状态,才有机会再次得到cpu timeslice 转到运行(running)状态。阻塞的状况分两种:
同步阻塞:运行(running)的线程进入了一个synchronized方法,若该同步锁被别的线程占用,则JVM会把该线程放入锁池(lock pool)中。
其余阻塞:运行(running)的线程发出了I/O请求时,JVM会把该线程置为阻塞状态。当I/O处理完毕时,线程从新转入可运行(runnable)状态。
线程run()、main() 方法执行结束,或者因异常退出了run()方法,则该线程结束生命周期。
jstack查看线程状态jstack -l <pid>
便可察看线程状态,如何使用呢?
随便写一个死循环看一下
public class TestThreadState { public static void main(String[] args) { for (; ; ) { } } }
ps -ef|grep TestThreadState
,找到对应的pid,jstack -l <pid>便可,若是未输出线程信息,能够尝试使用-F参数来强制输出。
"main" #1 prio=5 os_prio=31 tid=0x00007f8194801800 nid=0x1603 runnable [0x000070000a9b4000] java.lang.Thread.State: RUNNABLE at org.java.bin.TestThreadState.main(TestThreadState.java:12)