Java线程状态详解

首先Java的线程是通过Thread类中的内部类State来表示的,而在Thead类中,其实也有一个threadStates字段来表明当前线程的状态。Java是通过native()方法将threadStates映射成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;
}

由源码可知Java的线程有6种,其实其原理和操作系统中的线程原理几乎一样(下面有线程迁移):

NEW(新建状态)

        此时,该Thread类仅仅被创建,但是没有调用start()方法,故其还没有被启动;

RUNNABLE(就绪状态)

        此时,该Thread类调用start()方法后,该线程在JVM中为可运行状态,但是仍需根据操作系统中的底层调度来确定;

BLOCKED(阻塞状态)

        此时,该Thread类正在等待一个监视器锁(Java中的synchronized关键字)。这样就表明了此时该Thread类正在等待进入一个由synchronized标识的代码块或者方法;

WAITING(等待状态)

        此时,该Thread类正在等待某些能够继续运行的条件到达时Thread类所处的状态。

        假如调用了如下方法,则Thread将会进入等待状态:

       Object中定义的无超时的wait()方法:

                就例如学习操作系统时的wait()方法,等待一个同步监视器锁的唤醒,也就是等待这些资源的到达或者给予访问权限;

        Thread类中定义的无超时的join()方法:

                就是Thread类中的join方法,谁调用了join方法后,只有等待执行完成之后才可以进行其他线程的执行,所以遇到join()同样需要等待;

        LockSupport.park()方法:

                LockSupport类其拥有两个方法:unpark(提供许可)、park(等待许可),如果遇到了等待许可,只有什么时候获得了该许可才可以继续执行,所以其需要等待;

TIMED_WAITING(等待超时状态)

        其实就是在等待状态中新增了超时的限制,在调用一下方法将会进入等待超时状态:

                Thread.sleep()方法:将该线程休眠

                Object中定义的带有超时的wait()方法:参考上述

                Thread类中定义的带有超时的join()方法:参考上述

                LockSupport.parkNanos()方法;

                LockSupport.parkUntil()方法。

TERMINATED(终止状态)

        线程正常执行完毕和异常终止;

线程状态的迁移:

 


版权声明:本文为qq_58876428原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>