Thread.State

首先看JDK中的代码: java.lang.Thread.State

    /**
     * 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;
    }

1、新建(New)

新创建了一个线程对象,还未调用start()方法。

2、就绪(Runnable)

线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中 获取cpu 的使用权 。

3、运行中(Running,线程状态中并没有这一状态,但是实际执行中是有的)

可运行状态(runnable)的线程获得了cpu 时间片(timeslice) ,执行程序代码。

4、限期等待(Timed Waiting)

也可以称作 TIMED_WAITING(有等待时间的等待状态)。

线程主动调用以下方法:

Thread.sleep方法;

Object的wait方法,带有时间;

Thread.join方法,带有时间;

LockSupport的parkNanos方法,带有时间。

5、无限期等待(Waiting)

运行中(Running)的线程执行了以下方法:

Object的wait方法,并且没有使用timeout参数;

Thread的join方法,没有使用timeout参数;

LockSupport的park方法;

Conditon的await方法。

6、阻塞(Blocked)

阻塞状态是指线程因为某种原因放弃了cpu 使用权,暂时停止运行。直到线程进入可运行(runnable)状态,才有机会再次获得cpu timeslice 转到运行(running)状态。阻塞的情况分两种:

同步阻塞:运行(running)的线程进入了一个synchronized方法,若该同步锁被别的线程占用,则JVM会把该线程放入锁池(lock pool)中。

其他阻塞:运行(running)的线程发出了I/O请求时,JVM会把该线程置为阻塞状态。当I/O处理完毕时,线程重新转入可运行(runnable)状态。

7、结束(Terminated)

线程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)

菟潞寺沙弥
303 声望55 粉丝