美文网首页
线程的六种状态

线程的六种状态

作者: smallgrey | 来源:发表于2019-10-08 00:39 被阅读0次

线程一共有六种状态
NEW(新建状态:线程被创建没有调用start())
RUNNABLE(运行状态:Java线程把操作系统中的就绪和运行两种状态统一称为"运行中")
BLOCKED(阻塞状态:
A:等待阻塞(运行的线程执行了wait(),JVM会把当前线程放入等待队列 ),
B:同步阻塞(运行的线程在获取对象的同步锁的时,如果该同步锁被其他线程占用了,JVM会把当前线程放入锁池中),
C:其他阻塞(运行的线程执行sleep(),join()或者发出IO请求时,JVM会把当前线程设置为阻塞状态,当sleep()执行完,join()线程终止,IO处理完毕线程再恢复))
WAITING(等待状态)
TIMED_WAITING(超时等待:超时以后自动返回)
TERMINATED(终止状态)
以下是version1.5以上的代码源码
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;
    }

相关文章

  • java多线程

    线程六种状态 New:尚未启动的线程的线程状态(new Thread) Runnable:可运行线程的线程状态,等...

  • 线程的生命周期及其六种状态的转换

    线程的六种状态 线程的生命周期主要有以下六种状态: New(新创建) Runnable(可运行) Blocked(...

  • 线程的六种状态

    线程一共有六种状态NEW(新建状态:线程被创建没有调用start())RUNNABLE(运行状态:Java线程把操...

  • 并发编程(三):线程状态

    线程状态 现在线程的状态有五种和六种,五种是以操作系统层面描述的,六种是以Java Thread State枚举描...

  • 并发总结(三)关于线程状态

    (一) 线程共有六种状态 (二)与锁相关的线程状态 2.1 当线程在对象的锁池中竞争锁时,状态为 BLOCKED ...

  • java中的线程Thread(2)

    进入后面章节学习之前,我们继续了解线程相关概念。 线程状态说明及对应枚举类 线程的六种状态: NEW:新建状态,线...

  • 说一下Java线程状态及切换

    一、什么是Java线程状态 在Java程序中,用于描述Java线程的六种状态: 新建(NEW):当前线程,刚刚新建...

  • 1.1.2 线程的六种状态

    线程的六种状态 new了线程之后 new start了之后 runnable 使用了synchronizied ,...

  • 网易课第一章java基础

    1.1.1JAVA程序运行机制1.1.2线程的六种状态1.1.3正确的线程中止方法1.1.5线程通讯1.1.6线程...

  • Java 线程生命周期

    线程的六种状态: New Runnable Blocked Waiting Timed Waiting Termi...

网友评论

      本文标题:线程的六种状态

      本文链接:https://www.haomeiwen.com/subject/wcompctx.html