线程的6种状态

以下是java源码

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

一、NEW(新建状态)

尚未启动的线程的线程状态。

二、RUNNABLE(可运行状态)

可运行线程的线程状态。runnablestate中的一个线程正在Java虚拟机中执行,但它可能等待操作系统(如处理器)的其他资源。

三、BLOCKED(阻塞状态)

线程状态为等待监视器锁的线程阻塞。处于阻塞状态的线程正在等待监视器锁输入同步块/方法或调用Object.wait后重新输入同步块/方法。

四、WAITING(无时间等待状态)

等待线程的线程状态。由于调用以下方法之一,线程处于等待状态:

处于等待状态的线程正在等待另一个线程对一个特定动作执行操作。例如,在对象上调用object. wait()的线程正在等待另一个线程在该对象上调用object. notify()或object. notifyall()。调用thread .join()的线程正在等待指定的线程终止。

五、TIMED_WAITING(有时间等待状态)

具有指定等待时间的等待线程的线程状态。由于在指

六、TERMINATED(终止状态)

退出的线程处于这种状态。

总结:

线程在给定的时间点上只能处于一种状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。

猜你喜欢

转载自blog.csdn.net/Sweeneyzuo/article/details/84195833
今日推荐