概述

JDK提供了一个工具类Executors来非常方便的创建线程池,下面主要通过一个示例来分析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继承自Executor

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

列出了一部分的接口,主要是提供了几个启动线程执行线程任务的方法,接收不同的参数,以及关闭线程池的方法。submit方法接收Runnable或者Callable方法,返回一个Future对象用于异步获取执行结果。execute方法只接收一个Runnable参数,并且没有返回值。

Executors

再看一下Executors工具类的定义

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的抽象类。

抽象类中提供了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方法的时候只是将任务放到等待队列里面,核心线程不断从等待队列里面取出任务执行,没有任务的时候一直被堵塞住,当有任务来的时候直接取出执行,避免了不断创建线程带来的开销,以及增加了系统资源的利用率。

03-15 21:54