Java線程池ThreadPoolExecutor使用與解析

概述

JDK提供了一個工具類Executors來很是方便的建立線程池,下面主要經過一個示例來分析Java線程池的實現原理。java

使用

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // do something
    }
};

ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(runnable);
executorService.shutdown();

例子裏面使用了Executors.newFixedThreadPool(2)建立了一個固定只有2個線程的線程池,返回了一個ExecutorService對象,而後調用executorService.submit()方法來啓動一個線程,最後調用executorService.shutdown()來關閉線程池。編程

使用起來很是的方便,接下來經過深刻源代碼看一下背後的原理。併發

源碼分析

ExecutorService

看一下ExecutorService的定義異步

public interface ExecutorService extends Executor {
    void shutdown();
    List<Runnable> shutdownNow();
    boolean isShutdown();
    boolean isTerminated();
    boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
    <T> Future<T> submit(Callable<T> task);
    <T> Future<T> submit(Runnable task, T result);
    Future<?> submit(Runnable task);
    ...
}

ExecutorService繼承自Executoride

public interface Executor {
    void execute(Runnable command);
}

列出了一部分的接口,主要是提供了幾個啓動線程執行線程任務的方法,接收不一樣的參數,以及關閉線程池的方法。submit方法接收Runnable或者Callable方法,返回一個Future對象用於異步獲取執行結果。execute方法只接收一個Runnable參數,而且沒有返回值。工具

Executors

再看一下Executors工具類的定義oop

public class Executors {
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
    public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
    ...
}

大體是這個樣子的,這裏列出了一部分,提供了建立固定線程數的線程池(newFixedThreadPool),工做竊取的線程池(newWorkStealingPool),單個線程的線程池(newSingleThreadExecutor),不知道怎麼稱呼的線程池(newCachedThreadPool)。源碼分析

ThreadPoolExecutor

聲明

以FixedThreadPool爲例一探究竟,看一下FixedThreadPool返回的 ThreadPoolExecutor到底是什麼東西學習

public class ThreadPoolExecutor extends AbstractExecutorService {...}

public abstract class AbstractExecutorService implements ExecutorService {
    ...
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        return new FutureTask<T>(callable);
    }
    public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
    public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }
    ...
}

ThreadPoolExecutor繼承自AbstractExecutorService,AbstractExecutorService是一個實現了ExecutorService的抽象類。ui

抽象類中提供了submit方法的具體實現,將傳入的Runnable或者Callable方法經過newTaskFor方法轉換成一個FutureTask對象(它是RunnableFuture)的實現類,而後調用父類的execute方法執行任務,最終返回runnableFuture對象。從這能夠看出來ExecutorService.submit()方法內部仍是經過調用Executor.execute()方法來執行的,只是將參數轉換成一個Future對象,經過Future對象來獲取執行結果。

內部結構

/**
 * The runState provides the main lifecycle 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
 *    On invocation of shutdown(), perhaps implicitly in finalize()
 * (RUNNING or SHUTDOWN) -> STOP
 *    On invocation of shutdownNow()
 * SHUTDOWN -> TIDYING
 *    When both queue and pool are empty
 * STOP -> TIDYING
 *    When pool is empty
 * TIDYING -> TERMINATED
 *    When the terminated() hook method has completed
 */
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

// 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;

// Packing and unpacking ctl
private static int runStateOf(int c)     { return c & ~CAPACITY; }
private static int workerCountOf(int c)  { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }

定義了5種線程池的狀態

  • RUNNING 表示線程池能夠接受新的任務
  • SHUTDOWN 表示不接受新的任務,可是繼續處理隊列中的任務
  • STOP 表示不接受新的任務,中斷當前處理的任務和隊列種的任務
  • TIDYING 表示全部任務都已經停止了,全部線程都中止了,將要執行 terminated()方法以前的狀態
  • TERMINATED 表示 terminated()方法已經執行完了

有5種狀態變化的流程

  1. RUNNING -> SHUTDOWN 調用了 shutdown()
  2. (RUNNING or SHUTDOWN) -> STOP 調用了 shutdownNow()
  3. SHUTDOWN -> TIDYING 等待處理的隊列和線程池都爲空的時候
  4. STOP -> TIDYING 線程池爲空尚未執行terminated()以前的狀態
  5. TIDYING -> TERMINTED 已經執行完terminated()方法

AtomicInteger類型的ctl變量存着當前worker(Worker是一個內部類,下面會詳細解釋)的數量。

ThreadPoolExecutor用一個32位整型的高3位表示運行的狀態,剩下的29位表示能夠支持的線程數。

COUNT_BITS 爲32-3 = 29, 好比 RUNNING 是 -1 << COUNT_BITS,即-1帶符號位左移29位,就是101000...0,STOP爲001000...0,TIDYING爲010000...0。

CAPACITY 爲 (1 << COUNT_BITS) - 1,1左移29位以後-1,最後的結果位 0001111...1,最高3位是0 剩下的29位都是1。

workerCountOf(int c) 用來計算當前線程數,用的方法是 c & CAPACITY 即 c & 0001111...1,取除了高3位的剩下29位來判斷。

runStateOf(int c) 用來查看當前的線程狀態, c & ~CAPACITY 即 c & 1110000...0,取高3位來判斷。

private final BlockingQueue<Runnable> workQueue;
private final ReentrantLock mainLock = new ReentrantLock();
private final HashSet<Worker> workers = new HashSet<Worker>();
private final Condition termination = mainLock.newCondition();
private int largestPoolSize;
private long completedTaskCount;
private volatile ThreadFactory threadFactory;
private volatile RejectedExecutionHandler handler;
private volatile long keepAliveTime;
private volatile boolean allowCoreThreadTimeOut;
private volatile int corePoolSize;
private volatile int maximumPoolSize;
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
private static final RuntimePermission shutdownPerm = new RuntimePermission("modifyThread");

在來看一些其餘的全局屬性。workerQueue 一個BlockingQueue存放Runnable對象,workers 一個HashSet存放Worker對象,還有一些corePoolSize maximumPoolSize等就是平時配置鏈接池的參數。

構造方法

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

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.acc = System.getSecurityManager() == null ?
            null :
            AccessController.getContext();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}

Executors提供的newFIxedThreadPool方法其實建立的是一個ThreadPoolExecutor對象,以 newFixedPoolSize(2) 爲例,經過將corePoolSize maximumPoolSize都是設置爲2來實現固定數量的線程池。keepAliveTime設置爲0微秒。workerQueue傳入了一個LinkedBlockingQueue對象。

execute()方法

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();
    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))
            reject(command);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    else if (!addWorker(command, false))
        reject(command);
}

通讀幾遍代碼加上上面的註釋,基本能夠理解整個方法的意思。主要的思想是

  1. 當前線程數小於corePoolSize的時候調用addWorker來建立Worker類
  2. 當前線程數量大於corePoolSize的時候執行 workQueue.offer() 將任務加到等待隊列裏面
  3. 若是加不進等待隊列而且建立Worker失敗,就使用reject策略來拒絕當前任務

註釋中的第二點作了不少檢查,將任務加到等待隊列以後還要作一次檢查看看是否須要建立Worker,防止以前建立的Worker已經出現異常中止了。不理解不要緊,不影響對線程池原理的學習。

addWorker

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

        // Check if queue empty only if necessary.
        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
        }
    }

    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;
}

第一個for循環檢查線程數有沒有超過corePoolSize或者maximunPoolSize。過了這個for循環以後就是建立Worker的地方了

private final class Worker extends AbstractQueuedSynchronizer implements Runnable {

    /** 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);
    }

    public void run() {
        runWorker(this);
    }
    ...
}

Worker 類繼承自 AbstractQueuedSynchronizer 實現了 Runnable接口, AbstractQueuedSynchronizer 這個玩意特別厲害,是併發編程的核心類,因爲內容很是多本文不做解析。Worker類中維護了一個Thread對象,存了當前運行的線程,還維護了一個Runnable對象(firstTask),存了當前線程須要執行的對象。再回顧addWorker方法,其實就是用傳入的firstTask參數建立一個Worker對象,並使worker對象啓動一個線程去執行firstTask。重點在Worker對象的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);
    }
}

runWorker方法接受一個Worker參數,將參數裏面的firstTask拿出來,而後調用 task.run() 方法直接運行這個task,運行完將task變量設置爲null。而後這裏有一個while循環 while (task != null || (task = getTask()) != null),當task等於null的時候調用getTask()獲取任務。

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

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

        // Check if queue empty only if necessary.
        if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
            decrementWorkerCount();
            return null;
        }

        int wc = workerCountOf(c);

        // Are workers subject to culling?
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

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

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

getTask()方法裏面有一個死循環,boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; timed變量判斷wc變量是否大於corePoolSize (allowCoreThreadTimeOut 默認爲 false)。而後下面有一行代碼判斷timed時候爲ture,若是爲true,調用 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS),不然調用workQueue.take(),從等待隊列中獲取等待被處理的線程,而後返回出去。poll和take的區別是 當隊列裏面沒有數據的時候poll立刻返回false,而take會堵塞當前線程直到隊列裏面有數據。這裏解釋了爲何線程池可以維持線程不釋放。

總結

當設置了corePoolSize的時候,這個參數表明了可以運行的線程數,當用戶執行submit方法的時候首先會去判斷當前線程數有沒有達到corePoolSize,若是沒有達到,就建立Worker對象並啓動線程執行任務,一個對象內維護一個線程,當線程數超過corePoolSize的時候,用戶執行submit方法的時候只是將任務放到等待隊列裏面,核心線程不斷從等待隊列裏面取出任務執行,沒有任務的時候一直被堵塞住,當有任務來的時候直接取出執行,避免了不斷建立線程帶來的開銷,以及增長了系統資源的利用率。

相關文章
相關標籤/搜索