线程池源码分析:深入探索ThreadPoolExecutor

64 阅读6分钟
简介

在并发编程中,线程池是不可或缺的工具。允许我们复用线程资源,减少线程创建和销毁的开销,提高程序响应速度。在Java中,ThreadPoolExecutor是核心实现。

线程池预先创建一定数量的线程存放于容器中,当需要执行新任务时,从容器中取出一个线程执行任务,而不是每次都创建新的线程。

ThreadPoolExecutor 重要属性及方法

低29位保存线程池线程数,高3位保存线程池状态

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

  • 线程池线程数

    private static int workerCountOf(int c)  { return c & CAPACITY; }

  • 线程池状态

    private static int runStateOf(int c)     { return c & ~CAPACITY; }

  • RUNNING

    初始状态,此状态下可接收新任务和执行已添加任务

  • SHUTDOWN

    当调用**shutdown()**方法,由RUNNING 变为SHUTDOWN。

    此状态,不接收新任务,能继续处理已添加的任务

  • STOP

    调用shutdownNow(),由RUNNING或SHUTDOWN变为STOP。

    此状态,不接收任务,不处理已添加任务,尝试中断正在执行的任务

  • TIDYING

    所有任务已终止,且任务数量为0,线程池就会变为TIDYING。

    此状态下,线程池会执行函数terminated()

    状态转换:

  • SHUTDOWN下,阻塞队列为空且线程池中执行的任务也为空,由SHUTDOWN变为TIEDYING

  • STOP下,执行的任务为空,由STOP变为TIEDYING

  • TERMINATED

    执行完钩子函数,彻底终止,并进入TERMINATED

    最终状态,不在进行操作

  • 构造函数

    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;
      }
    
    
  • corePoolSize:基本线程数,空闲时也不会销毁(除非设置allowCoreThreadTimeOut,并且空闲时间超过keepAliveTime)

  • maximumPoolSize:线程池中允许最大线程数

  • keepAliveTime:线程数大于核心线程数时,空闲线程允许等待新任务最长时间

  • unit:keepAliveTime单位

  • workQueue:用于保存等待执行任务的阻塞队列

  • threadFactory:创建线程工厂

  • handler:无法处理新任务所用策略

  • CallerRunsPolicy:直接在调用execute方法的线程中运行该任务

  • AbortPolicy(默认):抛出RejectedExecutionException异常

  • DiscardPolicy:直接丢弃,不抛异常

  • DiscardOldestPolicy:丢弃队列中等待最长的任务,重新提交当前任务

源码分析
public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        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. 线程数小于核心线程数,创建核心线程,返回;

  2. RUNNING状态下,直接添加到队列中

    • 非running状态,任务移除队列,并执行拒绝策略

    • 再次检查线程数,为0,创建非核心线程

  3. 添加队列失败,创建非核心线程;

  4. 非核心线程创建失败,执行拒绝策略;

下面分析源码具体逻辑

addWorker(Runnable firstTask, boolean core):创建线程

private boolean addWorker(Runnable firstTask, boolean core) {
        // 1. 线程数+1
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // 校验
            // 非running && (shutdown && 任务为空或者队列为空),可以接收新任务,其他情况直接返回
            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            // 线程数+1
            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
            }
        }

        // 2. 创建线程
        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
                          // t为工作线程,仍然存活,但已shutdown并且没有执行的任务,这种情况不被允许的
                            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;
    }

分2部分

  • 增加线程数

    1. 校验:非running && (shutdown && 任务为空或者队列为空),可以接收新任务,其他情况直接返回

    2. 线程数+1

  • 创建线程

  • 创建线程添加到容器中,并启动当前线程

  • 异常处理:代码已注释,此时应该调用addWorkerFailed()

启动创建的线程,也就是Worker中的run方法

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())
                  // stop 尝试中断任务
                    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);
        }
    }

  • 执行当前任务或从队列中取任务执行

  • 最后处理当前这个线程:processWorkerExit

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())) {
                // stop 及之后不在处理任务 或 队列为空
                // 线程数-1
                decrementWorkerCount();
                return null;
            }

            int wc = workerCountOf(c);

            // Are workers subject to culling?
          // 允许核心线程超时或者大于核心线程数
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

            // 线程数大于 允许线程数 或 允许超时同时超时未获取到 现成数大于1或队列为空
            if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
               // 线程数-1
                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;
            }
        }
    }

  • 处于SHUTDOWN或STOP,并且队列为空,直接返回null

  • 超过核心线程数,允许超时,队列轮训获取任务,获取不到返回null

  • 如果没有超时限制,阻塞等待(take)获取一个任务

processWorkerExit:工作线程退出

private void processWorkerExit(Worker w, boolean completedAbruptly) {
    if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
    // 工作线程异常,线程数-1
        decrementWorkerCount();

    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        completedTaskCount += w.completedTasks;
        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);// 创建工作线程
    }
}

  • 异常退出:completedAbruptly(true),工作线程-1

  • 更新完成任务计数,移除工作线程

  • 调用tryTerminate,是否可关闭线程池

  • 是否需要添加线程

    • 状态低于STOP(接收新任务或处理队列任务)

    • 工作线程正常退出

      如果队列不为空,核心线程数为0,需要添加线程处理队列任务