純乾貨,源碼6步曲,帶你解析完整的ThreadPoolExecutor

ThreadPoolExecutor源碼解析

今天爲了給一個朋友作一份文檔,從源碼層級解析一下ThreadPoolExecutor。而後就直接在源碼上寫備註的形式解析,看這篇文章的朋友,就和看源碼同樣,一步步的跟着向下執行的看就好java

一、經常使用變量的解釋

// 1. `ctl`,能夠看作一個int類型的數字,高3位表示線程池狀態,低29位表示worker數量 private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // 2. `COUNT_BITS`,`Integer.SIZE`爲32,因此`COUNT_BITS`爲29 private static final int COUNT_BITS = Integer.SIZE - 3; // 3. `CAPACITY`,線程池容許的最大線程數。1左移29位,而後減1,即爲 2^29 - 1 private static final int CAPACITY   = (1 << COUNT_BITS) - 1; // runState is stored in the high-order bits // 4. 線程池有5種狀態,按大小排序以下:RUNNING < SHUTDOWN < STOP < TIDYING < TERMINATED 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; ​ // Packing and unpacking ctl // 5. `runStateOf()`,獲取線程池狀態,經過按位與操做,低29位將所有變成0 private static int runStateOf(int c)     { return c & ~CAPACITY; } // 6. `workerCountOf()`,獲取線程池worker數量,經過按位與操做,高3位將所有變成0 private static int workerCountOf(int c) { return c & CAPACITY; } // 7. `ctlOf()`,根據線程池狀態和線程池worker數量,生成ctl值 private static int ctlOf(int rs, int wc) { return rs | wc; } ​ /* * Bit field accessors that don't require unpacking ctl. * These depend on the bit layout and on workerCount being never negative. */ // 8. `runStateLessThan()`,線程池狀態小於xx private static boolean runStateLessThan(int c, int s) {    return c < s; } // 9. `runStateAtLeast()`,線程池狀態大於等於xx private static boolean runStateAtLeast(int c, int s) {    return c >= s; }

 

二、構造方法

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;    // 根據傳入參數`unit`和`keepAliveTime`,將存活時間轉換爲納秒存到變量`keepAliveTime `中    this.keepAliveTime = unit.toNanos(keepAliveTime);    this.threadFactory = threadFactory;    this.handler = handler; }

三、提交執行task的過程

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.     *     * 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.     *     * 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.     */    int c = ctl.get();    // worker數量比核心線程數小,直接建立worker執行任務    if (workerCountOf(c) < corePoolSize) {        if (addWorker(command, true))            return;        c = ctl.get();   }    // worker數量超過核心線程數,任務直接進入隊列    if (isRunning(c) && workQueue.offer(command)) {        int recheck = ctl.get();        // 線程池狀態不是RUNNING狀態,說明執行過shutdown命令,須要對新加入的任務執行reject()操做。        // 這兒爲何須要recheck,是由於任務入隊列先後,線程池的狀態可能會發生變化。        if (! isRunning(recheck) && remove(command))            reject(command);        // 這兒爲何須要判斷0值,主要是在線程池構造方法中,核心線程數容許爲0        else if (workerCountOf(recheck) == 0)            addWorker(null, false);   }    // 若是線程池不是運行狀態,或者任務進入隊列失敗,則嘗試建立worker執行任務。    // 這兒有3點須要注意:    // 1. 線程池不是運行狀態時,addWorker內部會判斷線程池狀態    // 2. addWorker第2個參數表示是否建立核心線程    // 3. addWorker返回false,則說明任務執行失敗,須要執行reject操做    else if (!addWorker(command, false))        reject(command); }

四、addworker源碼解析

private boolean addWorker(Runnable firstTask, boolean core) {    retry:    // 外層自旋    for (;;) {        int c = ctl.get();        int rs = runStateOf(c); ​        // 這個條件寫得比較難懂,我對其進行了調整,和下面的條件等價        // (rs > SHUTDOWN) ||        // (rs == SHUTDOWN && firstTask != null) ||        // (rs == SHUTDOWN && workQueue.isEmpty())        // 1. 線程池狀態大於SHUTDOWN時,直接返回false        // 2. 線程池狀態等於SHUTDOWN,且firstTask不爲null,直接返回false        // 3. 線程池狀態等於SHUTDOWN,且隊列爲空,直接返回false        // Check if queue empty only if necessary.        if (rs >= SHUTDOWN &&            ! (rs == SHUTDOWN &&               firstTask == null &&               ! workQueue.isEmpty()))            return false; ​        // 內層自旋        for (;;) {            int wc = workerCountOf(c);            // worker數量超過容量,直接返回false            if (wc >= CAPACITY ||                wc >= (core ? corePoolSize : maximumPoolSize))                return false;            // 使用CAS的方式增長worker數量。            // 若增長成功,則直接跳出外層循環進入到第二部分            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       }   }    boolean workerStarted = false;    boolean workerAdded = false;    Worker w = null;    try {        w = new Worker(firstTask);        final Thread t = w.thread;        if (t != null) {            final ReentrantLock mainLock = this.mainLock;            // worker的添加必須是串行的,所以須要加鎖            mainLock.lock();            try {                // Recheck while holding lock.                // Back out on ThreadFactory failure or if                // shut down before lock acquired.                // 這兒須要從新檢查線程池狀態                int rs = runStateOf(ctl.get()); ​                if (rs < SHUTDOWN ||                   (rs == SHUTDOWN && firstTask == null)) {                    // worker已經調用過了start()方法,則再也不建立worker                    if (t.isAlive()) // precheck that t is startable                        throw new IllegalThreadStateException();                    // worker建立並添加到workers成功                    workers.add(w);                    // 更新`largestPoolSize`變量                    int s = workers.size();                    if (s > largestPoolSize)                        largestPoolSize = s;                    workerAdded = true;               }           } finally {                mainLock.unlock();           }            // 啓動worker線程            if (workerAdded) {                t.start();                workerStarted = true;           }       }   } finally {        // worker線程啓動失敗,說明線程池狀態發生了變化(關閉操做被執行),須要進行shutdown相關操做        if (! workerStarted)            addWorkerFailed(w);   }    return workerStarted; }

五、線程池worker任務單元

private final class Worker    extends AbstractQueuedSynchronizer    implements Runnable {    /**     * This class will never be serialized, but we provide a     * serialVersionUID to suppress a javac warning.     */    private static final long serialVersionUID = 6138294804551838833L; ​    /** Thread this worker is running in. Null if factory fails. */    final Thread thread;    /** Initial task to run. Possibly null. */    Runnable firstTask;    /** Per-thread task counter */    volatile long completedTasks; ​    /**     * Creates with given first task and thread from ThreadFactory.     * @param firstTask the first task (null if none)     */    Worker(Runnable firstTask) {        setState(-1); // inhibit interrupts until runWorker        this.firstTask = firstTask;        // 這兒是Worker的關鍵所在,使用了線程工廠建立了一個線程。傳入的參數爲當前worker        this.thread = getThreadFactory().newThread(this);   } ​    /** Delegates main run loop to outer runWorker */    public void run() {        runWorker(this);   } ​    // 省略代碼... }

六、核心線程執行邏輯-runworker

final void runWorker(Worker w) {    Thread wt = Thread.currentThread();    Runnable task = w.firstTask;    w.firstTask = null;    // 調用unlock()是爲了讓外部能夠中斷    w.unlock(); // allow interrupts    // 這個變量用於判斷是否進入過自旋(while循環)    boolean completedAbruptly = true;    try {        // 這兒是自旋        // 1. 若是firstTask不爲null,則執行firstTask;        // 2. 若是firstTask爲null,則調用getTask()從隊列獲取任務。        // 3. 阻塞隊列的特性就是:當隊列爲空時,當前線程會被阻塞等待        while (task != null || (task = getTask()) != null) {            // 這兒對worker進行加鎖,是爲了達到下面的目的            // 1. 下降鎖範圍,提高性能            // 2. 保證每一個worker執行的任務是串行的            w.lock();            // If pool is stopping, ensure thread is interrupted;            // if not, ensure thread is not interrupted. This            // requires a recheck in second case to deal with            // shutdownNow race while clearing interrupt            // 若是線程池正在中止,則對當前線程進行中斷操做            if ((runStateAtLeast(ctl.get(), STOP) ||                 (Thread.interrupted() &&                  runStateAtLeast(ctl.get(), STOP))) &&                !wt.isInterrupted())                wt.interrupt();            // 執行任務,且在執行先後經過`beforeExecute()`和`afterExecute()`來擴展其功能。            // 這兩個方法在當前類裏面爲空實現。            try {                beforeExecute(wt, task);                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);               }           } finally {                // 幫助gc                task = null;                // 已完成任務數加一                w.completedTasks++;                w.unlock();           }       }        completedAbruptly = false;   } finally {        // 自旋操做被退出,說明線程池正在結束        processWorkerExit(w, completedAbruptly);   } }

怎麼樣,不知道你們看明白了沒有,可能對於有一些朋友來講有那麼一點點的困難,可是不要緊啊
既然已經說道thread了,咱也別落下什麼,相應的視頻合文檔已經整理完成git

關注公衆號:Java架構師聯盟,每日更新技術好文,添加小助手:msbxq2020免費獲取架構

部分資料已經上傳到個人git倉庫中:有須要的能夠下載ide

https://gitee.com/biwangsheng/mxqoop

相關文章
相關標籤/搜索