zoukankan      html  css  js  c++  java
  • 线程的几个状态转换

    java线程的几种状态转化图:

    Java线程一共有七个状态,分别是新建(New),就绪(Ready to run),运行中(Running),睡眠(Sleeping),阻塞(Blocked),等待(Waiting),死亡(Dead/Terminate)

    线程间的状态转化

    1、新建(new)

    新建一个线程,还未调用start方法

    Thread t = new Thread();

    2、就绪(runnable)

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

    3、运行中(running)

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

    4、睡眠(sleeping)

    也称TIME_WAITING(有等待时间的等待状态)

    线程主动调用这几个方法:

    1)Thread.sleep方法

    2)object的wait方法 带有时间

    3)Thread.join方法,带有时间

    4)LockSupport的parkNanos方法,带有时间

    5、阻塞(blocked)

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

    1)同步阻塞:运行的线程进入了一个sychronized方法,若该同步锁被别的线程占用,则jvm会把该线程放入锁池中

    2)其他阻塞:运行的线程发出了IO请求时,JVM会把该线程置为阻塞状态,当IO处理完毕后,线程重新转入可运行状态

    6、等待(waiting)

    运行中的线程执行了4个方法中的任意方法:

    1)Object的wait方法,并没有设置时间

    2)Thread的join方法,并没有设置时间

    3)Locksupport的park方法

    4)Condition的wait方法

    7、死亡(dead)

    线程run方法、main方法执行结束,或者因为异常退出了run方法,则该线程结束生命周期。死亡线程不可复生。

    /**
         * 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;
        }
  • 相关阅读:
    QT调用其他UI并使用QLabel(text)
    QT调用单例模式脚本
    QT 调用另一个UI实现方式
    QT 键值
    (一) Mybatis 源码解析之源码概述
    设计模式之 模板模式开发
    十二、线程池
    (十一)并发容器ConcurrentHashMap
    mybatis plus 踩坑记 -- 自动填充
    C/C++ file
  • 原文地址:https://www.cnblogs.com/cherish010/p/8610955.html
Copyright © 2011-2022 走看看