「查漏补缺」线程池源码分析

121 阅读6分钟

发现漏写了一些java相关源码博客,导致很久以前看过,有一点印象,但是讲流程就完全记不得,这次复习下,还是偏个人整理向。

参数说明

/**
 * 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) {
  • corePoolSize:核心线程数,是要保留在线程池中的线程数
  • maximumPoolSize:最大线程数,最多有多少个线程允许在池中
  • keepAliveTime:当线程数量大于核心线程数,这个时间就是多出来的部分等待任务的最长时间,超过这个时间就嘎了
  • unit:单位,服务上面个参数
  • workQueue:保留被执行前的线程
  • threadFactory:现成工厂
  • handler:拒绝策略

Demo

public static void test(){
    ExecutorService executor=new ThreadPoolExecutor(5, 10,
                    10L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10));
    for (int i = 0; i < 1000000; i++) {
        executor.submit(()->{
            System.out.println(1111);
        });
    }
}

分析

提交

代码

public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    //统一适配成RunnableFuture
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}
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.
       1. 如果正在运行的线程少于 corePoolSize,请尝试使用给定的命令作为第一个任务启动一个新线程。对 addWorker 的调用以原子方式检查 runState 和 workerCount,因此通过返回 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.
     * 2.如果任务可以成功添加进队列,那么我们还需要仔细检查是否应该添加线程
 (因为现有的自上次检查后死亡)或池自进入此方法后关闭。因此,我们重新检查状态,如果有必要,如果停止,则回滚排队,或者如果没有,则启动一个新线程。
     * 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.
     * 3.  如果我们不能排队任务,那么我们尝试添加一个新线程。如果它失败了,我们知道我们已经关闭或饱和,因此拒绝该任务。
     */
    int c = ctl.get();
    //小于核心线程数
    if (workerCountOf(c) < corePoolSize) {
        //添加worker,true:添加核心线程
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    //线程池处于运行状态,往阻塞队列扔任务
    if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        //线程池关闭,则移除刚刚添加的任务,走reject策略
        if (! isRunning(recheck) && remove(command))
            reject(command);
        //当一个worker都没有,添加worker    
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    //无法往队列塞了,创建非核心线程
    else if (!addWorker(command, false))
        //新添加的队列塞不下了,最大线程数也创建满worker了,就执行拒绝策略
        reject(command);
}

大致流程

  • 如果当前Worker(工作线程数后面用Worker代替)数量少于核心线程数,则创建Worker并返回
  • 如果当前Worker已经等于核心线程数,则尝试往阻塞队列塞
    • 再次检查线程池状态,如果异常执行拒绝策略
    • 如果没有Worker,添加非核心线程
  • 如果当前Worker已经等于核心线程数,阻塞队列也无法塞了,尝试创建非核心Worker
    • 如果阻塞队列也无法塞了,Worker也到了最大线程数了,就执行拒绝策略

添加Worker

代码

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 (;;) {
            //worker数量
            int wc = workerCountOf(c);
            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 {
        //封装成Woker
        w = new Worker(firstTask);
        //外部线程工厂创建再封装Worker,主义,t就是worker经过线程工厂封装后的
        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();
                    //添加进wokers(集合)    
                    workers.add(w);
                    int s = workers.size();
                    if (s > largestPoolSize)
                        largestPoolSize = s;
                    workerAdded = true;
                }
            } finally {
                mainLock.unlock();
            }
            //启动Worker
            if (workerAdded) {
                t.start();
                workerStarted = true;
            }
        }
    } finally {
        if (! workerStarted)
            addWorkerFailed(w);
    }
    return workerStarted;
}

大致流程

  • 增加worker计数
  • 添加worker进集合并启动

Worker内部

/**
 * Class Worker mainly maintains interrupt control state for
 * threads running tasks, along with other minor bookkeeping.
 * This class opportunistically extends AbstractQueuedSynchronizer
 * to simplify acquiring and releasing a lock surrounding each
 * task execution.  This protects against interrupts that are
 * intended to wake up a worker thread waiting for a task from
 * instead interrupting a task being run.  We implement a simple
 * non-reentrant mutual exclusion lock rather than use
 * ReentrantLock because we do not want worker tasks to be able to
 * reacquire the lock when they invoke pool control methods like
 * setCorePoolSize.  Additionally, to suppress interrupts until
 * the thread actually starts running tasks, we initialize lock
 * state to a negative value, and clear it upon start (in
 * runWorker).
 简单的说做了锁优化,更好并发及状态的控制,太多了,就不贴了
 */
private final class Worker
    extends AbstractQueuedSynchronizer
    implements Runnable
{


    /**
     * 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);
    }
    final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            //第一次就是外部传进来的,后面都是getTask获取
            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 {
                    //do nothing
                    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);
        }
    }

    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.
            //下面有图片,大于SHUTDOWN都是异常状态,开始走关闭流程
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }
            //worker数量
            int wc = workerCountOf(c);

            // Are workers subject to culling?
            // 线程数大于核心线程数
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
            // timedOut:如果从队列中没有获取到
            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;
            }
        }
    }
    //正常情况下只有减少非核心线程数的情况下才会进到
    private void processWorkerExit(Worker w, boolean completedAbruptly) {
        if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
            decrementWorkerCount();

        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            completedTaskCount += w.completedTasks;
            //移除Worker
            workers.remove(w);
        } finally {
            mainLock.unlock();
        }

        tryTerminate();

        int c = ctl.get();
        if (runStateLessThan(c, STOP)) {
            if (!completedAbruptly) {
                int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
                if (min == 0 && ! workQueue.isEmpty())
                    min = 1;
                if (workerCountOf(c) >= min)
                    return; // replacement not needed
            }
            addWorker(null, false);
        }
    }
    
}

image.png

大致流程

  • worker线程启动的时候,第一次正常执行
  • 后续通过阻塞队列解耦
    • 也是在这里判断是否要减少至核心线程数

总结

  • 如果当前Worker(工作线程数后面用Worker代替)数量少于核心线程数,则创建Worker并执行
  • 如果当前Worker已经等于核心线程数,则尝试往阻塞队列塞
    • 再次检查线程池状态,如果异常执行拒绝策略
    • 如果没有Worker,添加非核心线程
  • 如果当前Worker已经等于核心线程数,阻塞队列也无法塞了,尝试创建非核心Worker并执行
    • 如果阻塞队列也无法塞了,Worker也到了最大线程数(maximumPoolSize)了,就执行拒绝策略
  • worker第一次用用户传进来的任务执行,后续通过阻塞队列解耦
  • 通过阻塞队列超时判断是否要减少worker