1. 程式人生 > 其它 >執行緒狀態與執行緒池狀態

執行緒狀態與執行緒池狀態

執行緒狀態

NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED 參考java.lang.Thread類的內部列舉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; }

執行緒池狀態

http://concurrent.redspider.group/article/03/12.html 參考java.util.concurrent.ThreadPoolExecutor
// runState is stored in the high-order bits
private static final int RUNNING    = -1 << COUNT_BITS;
private static final int SHUTDOWN   =  0 << COUNT_BITS;
private static final int STOP       =  1 << COUNT_BITS;
private static final int TIDYING    =  2 << COUNT_BITS;
private static final int TERMINATED =  3 << COUNT_BITS;
  • 執行緒池建立後處於RUNNING狀態。
  • 呼叫shutdown()方法後處於SHUTDOWN狀態,執行緒池不能接受新的任務,清除一些空閒worker,會等待阻塞佇列的任務完成。
  • 呼叫shutdownNow()方法後處於STOP狀態,執行緒池不能接受新的任務,中斷所有執行緒,阻塞佇列中沒有被執行的任務全部丟棄。此時,poolsize=0,阻塞佇列的size也為0。
  • 當所有的任務已終止,ctl記錄的”任務數量”為0,執行緒池會變為TIDYING狀態。接著會執行terminated()函式。

ThreadPoolExecutor中有一個控制狀態的屬性叫ctl,它是一個AtomicInteger型別的變數。執行緒池狀態就是通過AtomicInteger型別的成員變數ctl來獲取的。獲取的ctl值傳入runStateOf方法,與~CAPACITY位與運算(CAPACITY是低29位全1的int變數)。~CAPACITY在這裡相當於掩碼,用來獲取ctl的高3位,表示執行緒池狀態;而另外的低29位用於表示工作執行緒數

附上runStateOf()方法,如下:

private static int runStateOf(int c)     { return c & ~CAPACITY; }
  • 執行緒池處在TIDYING狀態時,執行完terminated()方法之後,就會由 TIDYING -> TERMINATED, 執行緒池被設定為TERMINATED狀態。