深入淺出讓你理解Java線程池—ThreadPoolExecutor

幾句閒扯:首先,我想說java的線程池真的是很繞,以前一直都感覺新建幾個線程一直不退出到底是怎麼實現的,也就有了後來學習ThreadPoolExecutor源碼。學習源碼的過程中,最噁心的其實就是幾種狀態的轉換了,這也是ThreadPoolExecutor的核心。花了將近小一週才大致的弄明白ThreadPoolExecutor的機制,遂記錄下來。

線程池有多重要

線程是一個程序員一定會涉及到的一個概念,但是線程的創建和切換都是代價比較大的。所以,我們有沒有一個好的方案能做到線程的複用呢?這就涉及到一個概念——線程池。合理的使用線程池能夠帶來3個很明顯的好處:

  1. 降低資源消耗:通過重用已經創建的線程來降低線程創建和銷燬的消耗
  2. 提高響應速度:任務到達時不需要等待線程創建就可以立即執行。
  3. 提高線程的可管理性:線程池可以統一管理、分配、調優和監控。

java多線程池的支持——ThreadPoolExecutor

java的線程池支持主要通過ThreadPoolExecutor來實現,我們使用的ExecutorService的各種線程池策略都是基於ThreadPoolExecutor實現的,所以ThreadPoolExecutor十分重要。要弄明白各種線程池策略,必須先弄明白ThreadPoolExecutor。

1、實現原理

首先看一個線程池的流程圖:

深入淺出讓你理解Java線程池—ThreadPoolExecutor

  • step1.調用ThreadPoolExecutor的execute提交線程,首先檢查CorePool,如果CorePool內的線程小於CorePoolSize,新創建線程執行任務。
  • step2.如果當前CorePool內的線程大於等於CorePoolSize,那麼將線程加入到BlockingQueue。
  • step3.如果不能加入BlockingQueue,在小於MaxPoolSize的情況下創建線程執行任務。
  • step4.如果線程數大於等於MaxPoolSize,那麼執行拒絕策略。

2、線程池的創建

線程池的創建可以通過ThreadPoolExecutor的構造方法實現:

/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

具體解釋一下上述參數:

  1. corePoolSize 核心線程池大小
  2. maximumPoolSize 線程池最大容量大小
  3. keepAliveTime 線程池空閒時,線程存活的時間
  4. TimeUnit 時間單位
  5. ThreadFactory 線程工廠
  6. BlockingQueue任務隊列
  7. RejectedExecutionHandler 線程拒絕策略

3、線程的提交

ThreadPoolExecutor的構造方法如上所示,但是隻是做一些參數的初始化,ThreadPoolExecutor被初始化好之後便可以提交線程任務,線程的提交方法主要是execute和submit。這裏主要說execute,submit會在後續的博文中分析。

    /**
     * Executes the given task sometime in the future.  The task
     * may execute in a new thread or in an existing pooled thread.
     *
     * If the task cannot be submitted for execution, either because this
     * executor has been shutdown or because its capacity has been reached,
     * the task is handled by the current {@code RejectedExecutionHandler}.
     *
     * @param command the task to execute
     * @throws RejectedExecutionException at discretion of
     *         {@code RejectedExecutionHandler}, if the task
     *         cannot be accepted for execution
     * @throws NullPointerException if {@code command} is null
     */
    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         * 如果當前的線程數小於核心線程池的大小,根據現有的線程作爲第一個Worker運行的線程,
         * 新建一個Worker,addWorker自動的檢查當前線程池的狀態和Worker的數量,
         * 防止線程池在不能添加線程的狀態下添加線程
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *  如果線程入隊成功,然後還是要進行double-check的,因爲線程池在入隊之後狀態是可能會發生變化的
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         * 
         * 如果task不能入隊(隊列滿了),這時候嘗試增加一個新線程,如果增加失敗那麼當前的線程池狀態變化了或者線程池已經滿了
         * 然後拒絕task
         */
        int c = ctl.get();
        //當前的Worker的數量小於核心線程池大小時,新建一個Worker。
        if (workerCountOf(c) < corePoolSize) { 
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }

        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))//recheck防止線程池狀態的突變,如果突變,那麼將reject線程,防止workQueue中增加新線程
                reject(command);
            else if (workerCountOf(recheck) == 0)//上下兩個操作都有addWorker的操作,但是如果在workQueue.offer的時候Worker變爲0,
                                                //那麼將沒有Worker執行新的task,所以增加一個Worker.
                addWorker(null, false);
        }
        //如果workQueue滿了,那麼這時候可能還沒到線程池的maxnum,所以嘗試增加一個Worker
        else if (!addWorker(command, false))
            reject(command);//如果Worker數量到達上限,那麼就拒絕此線程
    }

這裏需要明確幾個概念

  • Worker和Task的區別,Worker是當前線程池中的線程,而task雖然是runnable,但是並沒有真正執行,只是被Worker調用了run方法,後面會看到這部分的實現。
  • maximumPoolSize和corePoolSize的區別:這個概念很重要,maximumPoolSize爲線程池最大容量,也就是說線程池最多能起多少Worker。corePoolSize是核心線程池的大小,當corePoolSize滿了時,同時workQueue full(ArrayBolckQueue是可能滿的) 那麼此時允許新建Worker去處理workQueue中的Task,但是不能超過maximumPoolSize。超過corePoolSize之外的線程會在空閒超時後終止。

核心方法:addWorker
Worker的增加和Task的獲取以及終止都是在此方法中實現的,也就是這一個方法裏面包含了很多東西。在addWorker方法中提到了Status的概念,Status是線程池的核心概念,這裏我們先看一段關於status的註釋:

/**
     * 首先ctl是一個原子量,同時它裏面包含了兩個field,一個是workerCount,另一個是runState
     * workerCount表示當前有效的線程數,也就是Worker的數量
     * runState表示當前線程池的狀態
     * The main pool control state, ctl, is an atomic integer packing
     * two conceptual fields
     *   workerCount, indicating the effective number of threads
     *   runState,    indicating whether running, shutting down etc
     * 
     * 兩者是怎麼結合的呢?首先workerCount是佔據着一個atomic integer的後29位的,而狀態佔據了前3位
     * 所以,workerCount上限是(2^29)-1。
     * In order to pack them into one int, we limit workerCount to
     * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
     * billion) otherwise representable. If this is ever an issue in
     * the future, the variable can be changed to be an AtomicLong,
     * and the shift/mask constants below adjusted. But until the need
     * arises, this code is a bit faster and simpler using an int.
     *
     * The workerCount is the number of workers that have been
     * permitted to start and not permitted to stop.  The value may be
     * transiently different from the actual number of live threads,
     * for example when a ThreadFactory fails to create a thread when
     * asked, and when exiting threads are still performing
     * bookkeeping before terminating. The user-visible pool size is
     * reported as the current size of the workers set.
     *
     * runState是整個線程池的運行生命週期,有如下取值:
     *  1. RUNNING:可以新加線程,同時可以處理queue中的線程。
     *  2. SHUTDOWN:不增加新線程,但是處理queue中的線程。
     *  3.STOP 不增加新線程,同時不處理queue中的線程。
     *  4.TIDYING 所有的線程都終止了(queue中),同時workerCount爲0,那麼此時進入TIDYING
     *  5.terminated()方法結束,變爲TERMINATED
     * The runState provides the main lifecyle control, taking on values:
     *
     *   RUNNING:  Accept new tasks and process queued tasks
     *   SHUTDOWN: Don't accept new tasks, but process queued tasks
     *   STOP:     Don't accept new tasks, don't process queued tasks,
     *             and interrupt in-progress tasks
     *   TIDYING:  All tasks have terminated, workerCount is zero,
     *             the thread transitioning to state TIDYING
     *             will run the terminated() hook method
     *   TERMINATED: terminated() has completed
     *
     * The numerical order among these values matters, to allow
     * ordered comparisons. The runState monotonically increases over
     * time, but need not hit each state. The transitions are:
     * 狀態的轉化主要是:
     * RUNNING -> SHUTDOWN(調用shutdown())
     *    On invocation of shutdown(), perhaps implicitly in finalize()
     * (RUNNING or SHUTDOWN) -> STOP(調用shutdownNow())
     *    On invocation of shutdownNow()
     * SHUTDOWN -> TIDYING(queue和pool均empty)
     *    When both queue and pool are empty
     * STOP -> TIDYING(pool empty,此時queue已經爲empty)
     *    When pool is empty
     * TIDYING -> TERMINATED(調用terminated())
     *    When the terminated() hook method has completed
     *
     * Threads waiting in awaitTermination() will return when the
     * state reaches TERMINATED.
     *
     * Detecting the transition from SHUTDOWN to TIDYING is less
     * straightforward than you'd like because the queue may become
     * empty after non-empty and vice versa during SHUTDOWN state, but
     * we can only terminate if, after seeing that it is empty, we see
     * that workerCount is 0 (which sometimes entails a recheck -- see
     * below).
     */

下面是狀態的代碼:

//利用ctl來保證當前線程池的狀態和當前的線程的數量。ps:低29位爲線程池容量,高3位爲線程狀態。
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    //設定偏移量
    private static final int COUNT_BITS = Integer.SIZE - 3;
    //確定最大的容量2^29-1
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;
    //幾個狀態,用Integer的高三位表示
    // runState is stored in the high-order bits
    //111
    private static final int RUNNING    = -1 << COUNT_BITS;
    //000
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    //001
    private static final int STOP       =  1 << COUNT_BITS;
    //010
    private static final int TIDYING    =  2 << COUNT_BITS;
    //011
    private static final int TERMINATED =  3 << COUNT_BITS;
    //獲取線程池狀態,取前三位
    // Packing and unpacking ctl
    private static int runStateOf(int c)     { return c & ~CAPACITY; }
    //獲取當前正在工作的worker,主要是取後面29位
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    //獲取ctl
    private static int ctlOf(int rs, int wc) { return rs | wc; }

接下來貼上addWorker方法看看:

    /**
     * Checks if a new worker can be added with respect to current
     * pool state and the given bound (either core or maximum). If so,
     * the worker count is adjusted accordingly, and, if possible, a
     * new worker is created and started running firstTask as its
     * first task. This method returns false if the pool is stopped or
     * eligible to shut down. It also returns false if the thread
     * factory fails to create a thread when asked, which requires a
     * backout of workerCount, and a recheck for termination, in case
     * the existence of this worker was holding up termination.
     *
     * @param firstTask the task the new thread should run first (or
     * null if none). Workers are created with an initial first task
     * (in method execute()) to bypass queuing when there are fewer
     * than corePoolSize threads (in which case we always start one),
     * or when the queue is full (in which case we must bypass queue).
     * Initially idle threads are usually created via
     * prestartCoreThread or to replace other dying workers.
     *
     * @param core if true use corePoolSize as bound, else
     * maximumPoolSize. (A boolean indicator is used here rather than a
     * value to ensure reads of fresh values after checking other pool
     * state).
     * @return true if successful
     */
    private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);
            // Check if queue empty only if necessary.
            /**
             * rs!=Shutdown || fistTask!=null || workCount.isEmpty
             * 如果當前的線程池的狀態>SHUTDOWN 那麼拒絕Worker的add 如果=SHUTDOWN
             * 那麼此時不能新加入不爲null的Task,如果在WorkCount爲empty的時候不能加入任何類型的Worker,
             * 如果不爲empty可以加入task爲null的Worker,增加消費的Worker
             */
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            for (;;) {
                int wc = workerCountOf(c);
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                if (compareAndIncrementWorkerCount(c))
                    break retry;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
        }

        Worker w = new Worker(firstTask);
        Thread t = w.thread;

        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            // Recheck while holding lock.
            // Back out on ThreadFactory failure or if
            // shut down before lock acquired.
            int c = ctl.get();
            int rs = runStateOf(c);
            /**
             * rs!=SHUTDOWN ||firstTask!=null
             * 
             * 同樣檢測當rs>SHUTDOWN時直接拒絕減小Wc,同時Terminate,如果爲SHUTDOWN同時firstTask不爲null的時候也要Terminate
             */
            if (t == null ||
                (rs >= SHUTDOWN &&
                 ! (rs == SHUTDOWN &&
                    firstTask == null))) {
                decrementWorkerCount();
                tryTerminate();
                return false;
            }

            workers.add(w);

            int s = workers.size();
            if (s > largestPoolSize)
                largestPoolSize = s;
        } finally {
            mainLock.unlock();
        }

        t.start();
        // It is possible (but unlikely) for a thread to have been
        // added to workers, but not yet started, during transition to
        // STOP, which could result in a rare missed interrupt,
        // because Thread.interrupt is not guaranteed to have any effect
        // on a non-yet-started Thread (see Thread#interrupt).
        //Stop或線程Interrupt的時候要中止所有的運行的Worker
        if (runStateOf(ctl.get()) == STOP && ! t.isInterrupted())
            t.interrupt();
        return true;
    }

addWorker中首先進行了一次線程池狀態的檢測:

 int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            //判斷當前線程池的狀態是不是已經shutdown,如果shutdown了拒絕線程加入
            //(rs!=SHUTDOWN || first!=null || workQueue.isEmpty())
            //如果rs不爲SHUTDOWN,此時狀態是STOP、TIDYING或TERMINATED,所以此時要拒絕請求
            //如果此時狀態爲SHUTDOWN,而傳入一個不爲null的線程,那麼需要拒絕
            //如果狀態爲SHUTDOWN,同時隊列中已經沒任務了,那麼拒絕掉
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

其實是比較難懂的,主要在線程池狀態判斷條件這裏:

  1. 如果是runing,那麼跳過if。
  2. 如果rs>=SHUTDOWN,同時不等於SHUTDOWN,即爲SHUTDOWN以上的狀態,那麼不接受新線程。
  3. 如果rs>=SHUTDOWN,同時等於SHUTDOWN,同時first!=null,那麼拒絕新線程,如果first==null,那麼可能是新增加線程消耗Queue中的線程。但是同時還要檢測workQueue是否isEmpty(),如果爲Empty,那麼隊列已空,不需要增加消耗線程,如果隊列沒有空那麼運行增加first=null的Worker。

從這裏是可以看出一些策略的

  • 首先,在rs>SHUTDOWN時,拒絕一切線程的增加,因爲STOP是會終止所有的線程,同時移除Queue中所有的待執行的線程的,所以也不需要增加first=null的Worker了
  • 其次,在SHUTDOWN狀態時,是不能增加first!=null的Worker的,同時即使first=null,但是此時Queue爲Empty也是不允許增加Worker的,SHUTDOWN下增加的Worker主要用於消耗Queue中的任務

SHUTDOWN狀態時,是不允許向workQueue中增加線程的,isRunning(c) && workQueue.offer(command) 每次在offer之前都要做狀態檢測,也就是線程池狀態變爲>=SHUTDOWN時不允許新線程進入線程池了

            for (;;) {
                int wc = workerCountOf(c);
                //如果當前的數量超過了CAPACITY,或者超過了corePoolSize和maximumPoolSize(試core而定)
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                //CAS嘗試增加線程數,如果失敗,證明有競爭,那麼重新到retry。
                if (compareAndIncrementWorkerCount(c))
                    break retry;
                c = ctl.get();  // Re-read ctl
                //判斷當前線程池的運行狀態
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }

這段代碼做了一個兼容,主要是沒有到corePoolSize 或maximumPoolSize上限時,那麼允許添加線程,CAS增加Worker的數量後,跳出循環。
接下來實例化Worker,實例化Worker其實是很關鍵的,後面會說。
因爲workers是HashSet線程不安全的,那麼此時需要加鎖,所以mainLock.lock(); 之後重新檢查線程池的狀態,如果狀態不正確,那麼減小Worker的數量,爲什麼tryTerminate()目前不大清楚。如果狀態正常,那麼添加Worker到workers。最後:

  if (runStateOf(ctl.get()) == STOP && ! t.isInterrupted())
            t.interrupt();

註釋說的很清楚,爲了能及時的中斷此Worker,因爲線程存在未Start的情況,此時是不能響應中斷的,如果此時status變爲STOP,則不能中斷線程。此處用作中斷線程之用。
接下來我們看Worker的方法:

 /**
         * Creates with given first task and thread from ThreadFactory.
         * @param firstTask the first task (null if none)
         */
        Worker(Runnable firstTask) {
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }

這裏可以看出Worker是對firstTask的包裝,並且Worker本身就是Runnable的,看上去真心很煩。
通過ThreadFactory爲Worker自己構建一個線程。
因爲Worker是Runnable類型的,所以是有run方法的,上面也看到了會調用t.start() 其實就是執行了run方法:

        /** Delegates main run loop to outer runWorker  */
        public void run() {
            runWorker(this);
        }

調用了runWorker:

/**
     * Main worker run loop.  Repeatedly gets tasks from queue and
     * executes them, while coping with a number of issues:
     * 1 Worker可能還是執行一個初始化的task——firstTask。
     *    但是有時也不需要這個初始化的task(可以爲null),只要pool在運行,就會
     *   通過getTask從隊列中獲取Task,如果返回null,那麼worker退出。
     *   另一種就是external拋出異常導致worker退出。
     * 1. We may start out with an initial task, in which case we
     * don't need to get the first one. Otherwise, as long as pool is
     * running, we get tasks from getTask. If it returns null then the
     * worker exits due to changed pool state or configuration
     * parameters.  Other exits result from exception throws in
     * external code, in which case completedAbruptly holds, which
     * usually leads processWorkerExit to replace this thread.
     * 
     * 
     * 2 在運行任何task之前,都需要對worker加鎖來防止other pool中斷worker。
     *   clearInterruptsForTaskRun保證除了線程池stop,那麼現場都沒有中斷標誌
     * 2. Before running any task, the lock is acquired to prevent
     * other pool interrupts while the task is executing, and
     * clearInterruptsForTaskRun called to ensure that unless pool is
     * stopping, this thread does not have its interrupt set.
     *
     * 3. Each task run is preceded by a call to beforeExecute, which
     * might throw an exception, in which case we cause thread to die
     * (breaking loop with completedAbruptly true) without processing
     * the task.
     *
     * 4. Assuming beforeExecute completes normally, we run the task,
     * gathering any of its thrown exceptions to send to
     * afterExecute. We separately handle RuntimeException, Error
     * (both of which the specs guarantee that we trap) and arbitrary
     * Throwables.  Because we cannot rethrow Throwables within
     * Runnable.run, we wrap them within Errors on the way out (to the
     * thread's UncaughtExceptionHandler).  Any thrown exception also
     * conservatively causes thread to die.
     *
     * 5. After task.run completes, we call afterExecute, which may
     * also throw an exception, which will also cause thread to
     * die. According to JLS Sec 14.20, this exception is the one that
     * will be in effect even if task.run throws.
     *
     * The net effect of the exception mechanics is that afterExecute
     * and the thread's UncaughtExceptionHandler have as accurate
     * information as we can provide about any problems encountered by
     * user code.
     *
     * @param w the worker
     */
    final void runWorker(Worker w) {
        Runnable task = w.firstTask;
        w.firstTask = null;
        //標識線程是不是異常終止的
        boolean completedAbruptly = true;
        try {
            //task不爲null情況是初始化worker時,如果task爲null,則去隊列中取線程--->getTask()
            while (task != null || (task = getTask()) != null) {
                w.lock();
                //獲取woker的鎖,防止線程被其他線程中斷
                clearInterruptsForTaskRun();//清楚所有中斷標記
                try {
                    beforeExecute(w.thread, task);//線程開始執行之前執行此方法,可以實現Worker未執行退出,本類中未實現
                    Throwable thrown = null;
                    try {
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);//線程執行後執行,可以實現標識Worker異常中斷的功能,本類中未實現
                    }
                } finally {
                    task = null;//運行過的task標null
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            //處理worker退出的邏輯
            processWorkerExit(w, completedAbruptly);
        }
    }

從上面代碼可以看出,execute的Task是被“包裝 ”了一層,線程啓動時是內部調用了Task的run方法。
接下來所有的核心集中在getTask()方法上:

/**
     * Performs blocking or timed wait for a task, depending on
     * current configuration settings, or returns null if this worker
     * must exit because of any of:
     * 1. There are more than maximumPoolSize workers (due to
     *    a call to setMaximumPoolSize).
     * 2. The pool is stopped.
     * 3. The pool is shutdown and the queue is empty.
     * 4. This worker timed out waiting for a task, and timed-out
     *    workers are subject to termination (that is,
     *    {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
     *    both before and after the timed wait.
     *
     * @return task, or null if the worker must exit, in which case
     *         workerCount is decremented
     *         
     *         
     *  隊列中獲取線程
     */
    private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            //當前狀態爲>stop時,不處理workQueue中的任務,同時減小worker的數量所以返回null,如果爲shutdown 同時workQueue已經empty了,同樣減小worker數量並返回null
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            boolean timed;      // Are workers subject to culling?

            for (;;) {
                int wc = workerCountOf(c);
                timed = allowCoreThreadTimeOut || wc > corePoolSize;

                if (wc <= maximumPoolSize && ! (timedOut && timed))
                    break;
                if (compareAndDecrementWorkerCount(c))
                    return null;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }

            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

這段代碼十分關鍵,首先看幾個局部變量:
boolean timedOut = false;
主要是判斷後面的poll是否要超時
boolean timed;
主要是標識着當前Worker超時是否要退出。wc > corePoolSize時需要減小空閒的Worker數,那麼timed爲true,但是wc <= corePoolSize時,不能減小核心線程數timed爲false。
timedOut初始爲false,如果timed爲true那麼使用poll取線程。如果正常返回,那麼返回取到的task。如果超時,證明worker空閒,同時worker超過了corePoolSize,需要刪除。返回r=null。則 timedOut = true。此時循環到wc <= maximumPoolSize && ! (timedOut && timed)時,減小worker數,並返回null,導致worker退出。如果線程數<= corePoolSize,那麼此時調用 workQueue.take(),沒有線程獲取到時將一直阻塞,知道獲取到線程或者中斷,關於中斷後面Shutdown的時候會說。

至此線程執行過程就分析完了

關於終止線程池

我個人認爲,如果想了解明白線程池,那麼就一定要理解好各個狀態之間的轉換,想理解轉換,線程池的終止機制是很好的一個途徑。對於關閉線程池主要有兩個方法shutdown()和shutdownNow():
首先從shutdown()方法開始:

    /**
     * Initiates an orderly shutdown in which previously submitted
     * tasks are executed, but no new tasks will be accepted.
     * Invocation has no additional effect if already shut down.
     *
     * <p>This method does not wait for previously submitted tasks to
     * complete execution.  Use {@link #awaitTermination awaitTermination}
     * to do that.
     *
     * @throws SecurityException {@inheritDoc}
     */
    public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            //判斷是否可以操作目標線程
            checkShutdownAccess();
            //設置線程池狀態爲SHUTDOWN,此處之後,線程池中不會增加新Task
            advanceRunState(SHUTDOWN);
            //中斷所有的空閒線程
            interruptIdleWorkers();
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        //轉到Terminate
        tryTerminate();
    }

shutdown做了幾件事:
1. 檢查是否能操作目標線程
2. 將線程池狀態轉爲SHUTDOWN
3. 中斷所有空閒線程

這裏就引發了一個問題,什麼是空閒線程?
這需要接着看看interruptIdleWorkers是怎麼回事。

 private void interruptIdleWorkers(boolean onlyOne) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        //這裏的意圖很簡單,遍歷workers 對所有worker做中斷處理。
        // w.tryLock()對Worker加鎖,這保證了正在運行執行Task的Worker不會被中斷,那麼能中斷哪些線程呢?
        try {
            for (Worker w : workers) {
                Thread t = w.thread;
                if (!t.isInterrupted() && w.tryLock()) {
                    try {
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    } finally {
                        w.unlock();
                    }
                }
                if (onlyOne)
                    break;
            }
        } finally {
            mainLock.unlock();
        }
    }

這裏主要是爲了中斷worker,但是中斷之前需要先獲取鎖,這就意味着正在運行的Worker不能中斷。但是上面的代碼有w.tryLock(),那麼獲取不到鎖就不會中斷,shutdown的Interrupt只是對所有的空閒Worker(正在從workQueue中取Task,此時Worker沒有加鎖)發送中斷信號。

            while (task != null || (task = getTask()) != null) {
                w.lock();
                //獲取woker的鎖,防止線程被其他線程中斷
                clearInterruptsForTaskRun();//清楚所有中斷標記
                try {
                    beforeExecute(w.thread, task);//線程開始執行之前執行此方法,可以實現Worker未執行退出,本類中未實現
                    Throwable thrown = null;
                    try {
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);//線程執行後執行,可以實現標識Worker異常中斷的功能,本類中未實現
                    }
                } finally {
                    task = null;//運行過的task標null
                    w.completedTasks++;
                    w.unlock();
                }
            }

在runWorker中,每一個Worker getTask成功之後都要獲取Worker的鎖之後運行,也就是說運行中的Worker不會中斷。因爲核心線程一般在空閒的時候會一直阻塞在獲取Task上,也只有中斷纔可能導致其退出。這些阻塞着的Worker就是空閒的線程(當然,非核心線程,並且阻塞的也是空閒線程)。在getTask方法中:

    private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            //當前狀態爲>stop時,不處理workQueue中的任務,同時減小worker的數量所以返回null,如果爲shutdown 同時workQueue已經empty了,同樣減小worker數量並返回null
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            boolean timed;      // Are workers subject to culling?

            for (;;) {
                //allowCoreThreadTimeOu是判斷CoreThread是否會超時的,true爲會超時,false不會超時。默認爲false
                int wc = workerCountOf(c);
                timed = allowCoreThreadTimeOut || wc > corePoolSize;

                if (wc <= maximumPoolSize && ! (timedOut && timed))
                    break;
                if (compareAndDecrementWorkerCount(c))
                    return null;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }

            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

會有兩階段的Worker:

  • 剛進入getTask(),還沒進行狀態判斷。
  • block在poll或者take上的Worker。

當調用ShutDown方法時,首先設置了線程池的狀態爲ShutDown,此時1階段的worker進入到狀態判斷時會返回null,此時Worker退出。
因爲getTask的時候是不加鎖的,所以在shutdown時可以調用worker.Interrupt.此時會中斷退出,Loop到狀態判斷時,同時workQueue爲empty。那麼拋出中斷異常,導致重新Loop,在檢測線程池狀態時,Worker退出。如果workQueue不爲null就不會退出,此處有些疑問,因爲沒有看見中斷標誌位清除的邏輯,那麼這裏就會不停的循環直到workQueue爲Empty退出。
這裏也能看出來SHUTDOWN只是清除一些空閒Worker,並且拒絕新Task加入,對於workQueue中的線程還是繼續處理的
對於shutdown中獲取mainLock而addWorker中也做了mainLock的獲取,這麼做主要是因爲Works是HashSet類型的,是線程不安全的,我們也看到在addWorker後面也是對線程池狀態做了判斷,將Worker添加和中斷邏輯分離開。
接下來做了tryTerminate()操作,這操作是進行了後面狀態的轉換,在shutdownNow後面說。
接下來看看shutdownNow:

    /**
     * Attempts to stop all actively executing tasks, halts the
     * processing of waiting tasks, and returns a list of the tasks
     * that were awaiting execution. These tasks are drained (removed)
     * from the task queue upon return from this method.
     *
     * <p>This method does not wait for actively executing tasks to
     * terminate.  Use {@link #awaitTermination awaitTermination} to
     * do that.
     *
     * <p>There are no guarantees beyond best-effort attempts to stop
     * processing actively executing tasks.  This implementation
     * cancels tasks via {@link Thread#interrupt}, so any task that
     * fails to respond to interrupts may never terminate.
     *
     * @throws SecurityException {@inheritDoc}
     */
    public List<Runnable> shutdownNow() {
        List<Runnable> tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(STOP);
            interruptWorkers();
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
        return tasks;
    }

shutdownNow和shutdown代碼類似,但是實現卻很不相同。首先是設置線程池狀態爲STOP,前面的代碼我們可以看到,是對SHUTDOWN有一些額外的判斷邏輯,但是對於>=STOP,基本都是reject,STOP也是比SHUTDOWN更加嚴格的一種狀態。此時不會有新Worker加入,所有剛執行完一個線程後去GetTask的Worker都會退出。
之後調用interruptWorkers:

    /**
     * Interrupts all threads, even if active. Ignores SecurityExceptions
     * (in which case some threads may remain uninterrupted).
     */
    private void interruptWorkers() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers) {
                try {
                    w.thread.interrupt();
                } catch (SecurityException ignore) {
                }
            }
        } finally {
            mainLock.unlock();
        }
    }

這裏可以看出來,此方法目的是中斷所有的Worker,而不是像shutdown中那樣只中斷空閒線程。這樣體現了STOP的特點,中斷所有線程,同時workQueue中的Task也不會執行了。所以接下來drainQueue:

   /**
     * Drains the task queue into a new list, normally using
     * drainTo. But if the queue is a DelayQueue or any other kind of
     * queue for which poll or drainTo may fail to remove some
     * elements, it deletes them one by one.
     */
    private List<Runnable> drainQueue() {
        BlockingQueue<Runnable> q = workQueue;
        List<Runnable> taskList = new ArrayList<Runnable>();
        q.drainTo(taskList);
        if (!q.isEmpty()) {
            for (Runnable r : q.toArray(new Runnable[0])) {
                if (q.remove(r))
                    taskList.add(r);
            }
        }
        return taskList;
    }

獲取所有沒有執行的Task,並且返回。
這也體現了STOP的特點:
拒絕所有新Task的加入,同時中斷所有線程,WorkerQueue中沒有執行的線程全部拋棄。所以此時Pool是空的,WorkerQueue也是空的
這之後就是進行到TIDYING和TERMINATED的轉化了:

    /**
     * Transitions to TERMINATED state if either (SHUTDOWN and pool
     * and queue empty) or (STOP and pool empty).  If otherwise
     * eligible to terminate but workerCount is nonzero, interrupts an
     * idle worker to ensure that shutdown signals propagate. This
     * method must be called following any action that might make
     * termination possible -- reducing worker count or removing tasks
     * from the queue during shutdown. The method is non-private to
     * allow access from ScheduledThreadPoolExecutor.
     */
    final void tryTerminate() {
        for (;;) {
            int c = ctl.get();
            if (isRunning(c) ||
                runStateAtLeast(c, TIDYING) ||
                (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
                return;
            if (workerCountOf(c) != 0) { // Eligible to terminate
                interruptIdleWorkers(ONLY_ONE);
                return;
            }

            final ReentrantLock mainLock = this.mainLock;
            mainLock.lock();
            try {
                if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
                    try {
                        terminated();
                    } finally {
                        ctl.set(ctlOf(TERMINATED, 0));
                        termination.signalAll();
                    }
                    return;
                }
            } finally {
                mainLock.unlock();
            }
            // else retry on failed CAS
        }
    }

上面的代碼其實很有意思有幾種狀態是不能轉化到TIDYING的:

  1. RUNNING狀態
  2. TIDYING或TERMINATED
  3. SHUTDOWN狀態,但是workQueue不爲空

也說明了兩點:
1. SHUTDOWN想轉化爲TIDYING,需要workQueue爲空,同時workerCount爲0
2. STOP轉化爲TIDYING,需要workerCount爲0

如果滿足上面的條件(一般一定時間後都會滿足的),那麼CAS成TIDYING,TIDYING也只是個過度狀態,最終會轉化爲TERMINATED。

至此,ThreadPoolExecutor一些核心思想就介紹完了,想分析清楚實在是不容易,對於ThreadPoolExecutor我還是有些不懂地方,以上只是我對源碼的片面的見解,如果有不正確之處,希望各位大佬指出

共同進步,學習分享

歡迎大家關注我的公衆號【風平浪靜如碼】,海量Java相關文章,學習資料都會在裏面更新,整理的資料也會放在裏面。

覺得寫的還不錯的就點個贊,加個關注唄!點關注,不迷路,持續更新!!!

海量面試、架構資料分享

深入淺出讓你理解Java線程池—ThreadPoolExecutor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章