Java併發系列之7 深入理解線程池ThreadPoolExecutor

1. 初識線程池

線程池解決了如下兩個問題

  1. 當執行大量的異步任務時,線程池可以減少每個任務的調用切換開銷從而提高應用性能
  2. 對執行的線程,和要被執行的任務,提供了管理的方法

此外每個線程池還維護了一些基本統計信息,比如已完成任務的數量

2. ThreadPoolExecutor的簡單使用

我們創建一個線程池對象ThreadPoolExecutor,讓線程池執行10個打印任務,輸出當前任務名稱以及線程的名稱

public class ThreadPoolExecutorTest {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,5,0L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(10));
        for(int i=0;i<10;i++){
            final int num = i;
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("execute task "+num+" in "+Thread.currentThread().getName());
                }
            });
        }
    }
}

代碼還是蠻簡單的。首先創建一個ThreadPoolExecutor對象,然後調用ThreadPoolExecutor.execute(Runnable runnable)方法。輸出結果如下。我們可以觀察到線程池啓動了兩個線程pool-1-thread-1和pool-1-thread-2來執行任務。那麼這裏提兩個問題

  1. 該線程池最多能啓動5個線程,爲什麼線程池只啓動了2個線程來執行任務?
  2. 如果把for循環次數由10改成100,線程池會啓動幾個線程呢?

如果你還不是很有把握回答這兩個問題那麼請接着看下文分析吧。

execute task 0 in pool-1-thread-1
execute task 1 in pool-1-thread-2
execute task 2 in pool-1-thread-1
execute task 3 in pool-1-thread-2
execute task 5 in pool-1-thread-2
execute task 4 in pool-1-thread-1
execute task 6 in pool-1-thread-2
execute task 7 in pool-1-thread-1
execute task 8 in pool-1-thread-2
execute task 9 in pool-1-thread-1

3. ThreadPoolExecutor的成員變量和構造函數

 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

線程池中的線程被邏輯分爲兩類(核心線程和非核心線程)。核心線程如果被啓動了,一般情況下是會一直活着的(除非allowCoreThreadTimeOut被設置成true),非核心線程在keepAlivTime時間範圍內,如果沒有任務執行會被系統中斷回收掉。

那麼核心線程是在什麼時候被創建並啓動的呢?非核心線程又是在什麼時候被創建並啓動的呢?(稍安勿躁,後面會給出答案)

workQueue是指任務的集合。我們可以把ThreadPoolExecutor簡單的認爲它只有兩個比較重要的屬性 線程的集合和任務的集合。

private final HashSet<Worker> workers = new HashSet<>();

private final BlockingQueue<Runnable> workQueue;
  1. workers對象正是線程的集合,後面我們會對Worker對象做一個詳細的講解
  2. workQueue是一個阻塞隊列。阻塞隊列的概念就是如果隊列滿了那麼put操作會阻塞,如果隊列爲空那麼take操作會阻塞。

線程池的工作原理我們可以簡單地認爲是往workQueue裏面put任務,系統會合理的調度workers中的線程來處理workQueue中的任務。

下面我們來對構造函數的各個參數做一個詳細的介紹

  1. corePoolSize:核心線程個數,當新的任務通過execute(java.lang.Runnable)方法提交到線程池中,如果線程池的線程個數小於corePoolSize設定的值時,不管線程池中的線程是否空閒,都會新建線程來處理該任務
  2. maximumPoolSize:線程池最多允許的線程數。如果線程池中的線程數量大於等於corePoolSize而且小於等於maximumPoolSize,如果此時workQueue已滿,則會創建新的線程來處理任務(如果線程數等於maximumPoolSize則會執行相應的拒絕策略),反之如果workQueue未滿,則將該任務put到workQueue中。
  3. keepAliveTime:允許線程空閒的時間,如果空閒時間超過,而且線程池中的線程數比corePoolSize數量多,則會中斷和回收空閒線程
  4. unit:keepAliveTime的時間單位,秒或者毫秒等
  5. workQueue:工作隊列可以用來交付和保存提交到線程池中的工作任務,它和corePoolSize maximumPoolSize相互作用

處理任務的流程如下

  • 如果線程池中的線程數量小於corePoolSize,線程池將新建新的線程處理該任務,而不是將任務入隊列
  • 如果線程池中的線程數量大於等於corePoolSize,線程池將優先選擇將任務入隊列
  • 如果入隊列失敗(隊列已滿),將創建新的線程處理任務,除非線程超過了maximumPoolSize,任務將被拒絕

三種不同的入隊策略

  • 直接交付,使用 SynchronousQueue直接交付任務,而不是把任務保存在隊列中。
  • 無界的隊列,比如LinkedBlockingQueue,用無界隊列構建的線程池,線程數永遠不會超過corePoolSize
  • 有界的隊列,比如ArrayBlockingQueue,這種情況比較複雜,線程池同時受corePoolSize,maximumPoolSize,workQueue大小約束
  1. threadFactory:主要是給線程池中的線程命名,以便查找問題
  2. handler:線程池無法處理任務時的拒絕策略

4. execute(java.lang.Runnable)方法

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();
        //1 如果線程池中的線程數小於corePoolSize,新建線程處理command
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        //2 如果線程池中的線程數量大於等於corePoolSize,將任務入隊列
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        //3 如果任務入隊列失敗,創建非核心線程處理任務
        else if (!addWorker(command, false))
            reject(command);//4.如果創建非核心線程失敗,拒絕該任務
    }
  1. 如果線程池中的線程數小於corePoolSize,新建線程處理command
  2. 如果線程池中的線程數量大於等於corePoolSize,將任務入隊列
  3. 如果任務入隊列失敗,創建非核心線程處理任務
  4. 如果創建非核心線程失敗,拒絕該任務

5.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;
            this.thread = getThreadFactory().newThread(this);
        }

        /** Delegates main run loop to outer runWorker. */
        public void run() {
            runWorker(this);
        }
        
        final void runWorker(Worker w) {
            Thread wt = Thread.currentThread();
            Runnable task = w.firstTask;
            w.firstTask = null;
            w.unlock(); // allow interrupts
            boolean completedAbruptly = true;
            try {
                while (task != null || (task = getTask()) != null) {
                    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();
                    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 {
                        task = null;
                        w.completedTasks++;
                        w.unlock();
                    }
                 }
                completedAbruptly = false;
            } finally {
                processWorkerExit(w, completedAbruptly);
            }
    }
    }
  1. Worker實現了Runnable接口,構造函數中this.thread = getThreadFactory().newThread(this),啓動線程將執行run方法
  2. Worker是AbstractQueuedSynchronizer的子類,說明了Worker本身是一把鎖。如何判斷工作線程是否空閒?就是通過work.tryLock()來判斷不信可以看下interruptIdleWorkers(boolean onlyOne)方法,該方法的功能是中斷空閒的工作線程
private void interruptIdleWorkers(boolean onlyOne) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers) {
                Thread t = w.thread;
                //如果w.tryLock()返回true表示該工作線程處於空閒狀態
                if (!t.isInterrupted() && w.tryLock()) {
                    try {
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    } finally {
                        w.unlock();
                    }
                }
                if (onlyOne)
                    break;
            }
        } finally {
            mainLock.unlock();
        }
    }

我們來看下w.lock()調用的地方。在runWorker(Worker worker)方法中,在獲取到任務去處理時,會調用w.lock()

final void runWorker(Worker w) {
            Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
        //從任務隊列中獲取任務,可能會阻塞,因爲BlockingQueue是阻塞隊列
            while (task != null || (task = getTask()) != null) {
                //如果獲取到了任務去執行,上鎖
                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();
                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 {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

6. 工作線程什麼時候啓動的呢

在講execute(Runnable runnable)的方法的時候,創建工作線程是通過addWorker(Runnable firstTask, boolean core)方法實現的

private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            //如果線程池被shutDown,直接返回
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            for (;;) {
                int wc = workerCountOf(c);
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    //如果線程數超過了限制直接返回false
                    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
            }
        }

        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;
                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)) {
                        if (t.isAlive()) // precheck that t is startable
                            throw new IllegalThreadStateException();
                        workers.add(w);
                        int s = workers.size();
                        if (s > largestPoolSize)
                            largestPoolSize = s;
                        workerAdded = true;
                    }
                } finally {
                    mainLock.unlock();
                }
                if (workerAdded) {
                    //啓動工作線程
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }
  1. 判斷線程池是否滿足條件,如果不滿足返回false
  2. 創建Worker對象,並啓動工作線程

7. keepAliveTime功能是如何實現的

如何實現在keepAliveTime時間內空閒,中斷線程的呢,答案在getTask()中

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

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

            //1.如果調用了shutDownNow返回null
            //2.如果調用了shutDown而且workQueue沒有任務返回null
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            int wc = workerCountOf(c);

            //如果允許殺死空閒的核心線程,或者線程數超過corePoolSize
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

            if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
                if (compareAndDecrementWorkerCount(c))
                    return null;
                continue;
            }

            try {
            //如果指定時間內獲取不到任務,返回null,線程將會結束
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }
  1. 判斷是否需要殺死空閒的線程
  • 允許殺死核心線程 返回true
  • 不允許殺死核心線程 判斷線程數是否大於corePoolSize
  1. 通過workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)方法獲取任務,如果超時,返回null
  2. runWorker中getTask返回null,跳出循環,調用processWorkerExit(w, completedAbruptly)

8. shutdown和shutdownNow的區別

  1. shutdown方法調用後,不會處理新的任務,但是會處理已經進入隊列的任務
  2. shutdownNow方法調用後,不會處理新的任務,而且不會處理已經進入隊列的任務,而且會停止所有的工作線程
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章