• Java线程状态


    Java线程的状态分为NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED,通过枚举类java.lang.Thread.State定义。

        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表示线程创建后尚未启动

    2.RUNNABLE表示线程处于可运行状态,有可能正在被CPU执行或者等待CPU调度

    3.BLOCKED表示阻塞状态,表明线程正在等待监视器锁。当线程正在等待监视器锁进入synchronized块/方法时,或调用Object.wait方法后重新进入synchronized块/方法时。

    4.WAITING表示无限期等待状态。处于这种状态的线程不会被分配CPU执行时间,它们要等待显示的被其它线程唤醒。

    以下方法会让线程陷入无限期等待状态:

    (1)没有设置timeout参数的Object.wait()

    (2)没有设置timeout参数的Thread.join()

    (3)LockSupport.park()

    5.TIMED_WAITING表示超时等待状态。处于这种状态的线程也不会被分配CPU执行时间,不过无需等待被其它线程显示的唤醒,在一定时间之后它们会由系统自动的唤醒。

    以下方法会让线程进入TIMED_WAITING超时等待状态:

    (1)Thread.sleep()方法

    (2)设置了timeout参数的Object.wait()方法

    (3)设置了timeout参数的Thread.join()方法

    (4)LockSupport.parkNanos()方法

    (5)LockSupport.parkUntil()方法

    6.TERMINATED表示结束状态,表示线程已经结束执行

  • 相关阅读:
    JZOJ 4043. 【雅礼集训2015Kzf】洪水
    JZOJ 5451.Genocide
    P4719 【模板】"动态 DP"&动态树分治
    [NOIP2018 提高组] 保卫王国
    【NOIP2017提高组正式赛】列队
    vuex的总结
    height:100vh
    从URL输入到页面展现到底发生什么
    JS的空数组是true还是false(内附JS类型转换表)
    Vue拼图验证组件使用教程
  • 原文地址:https://www.cnblogs.com/huangzy/p/10747361.html
Copyright © 2020-2023  润新知