Java线程池原理浅析

一、线程池工厂Executors

我们平时在使用线程池的时候一般都是通过Executors的newXxxxxPool()静态方法来获得不同功能的线程池对象。我们来看一下这些方法都是怎么创建线程池的:

     /**
      * 固定线程数的线程池
      */
    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue(),
                                      threadFactory);
    }
     /**
      * 只有一个线程的线程池
      */
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue()));
    }
     /**
      * 可变大小线程池
      */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue());
    }
     /**
      *定时执行的线程池
      */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

我们可以看到这newFixedThreadPool()、newSingleThreadExecutor()、newCachedThreadPool()都是创建了一个ThreadPoolExecutor对象,而newScheduledThreadPool()则是创建了一个ScheduledThreadPoolExecutor对象,其实ScheduledThreadPoolExecutor也是继承了ThreadPoolExecutor这个类,通过在ThreadPoolExecutor上扩展实现了定时执行线程的功能。

ThreadPoolExecutor

我们先来看一下ThreadPoolExecutor的构造方法:

    /**
     * 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:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@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 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.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }

corePoolSize:线程池里最小线程数
maximumPoolSize:线程池里最大线程数
keepAliveTime:空闲线程存活的时间,也就是线程池的线程数超过corePoolSize后,空闲线程可以存活的时间,超过这个时间就会被销毁。
unit: keepAliveTime的单位
workQueue:用来存放等待任务的队列。这个队列是个阻塞队列
threadFactory:用来产生线程池里的线程的工厂
handler:当任务超过最大允许的任务数量后,新来任务的拒绝策略。
知道了上面几个参数,我们对ThreadPoolExecutor应该有所了解,对Executors产生的不同功能的线程池也应该有所了解。我们接下来讨论一下ThreadPoolExecutor实现线程池的原理。
首先从提交任务的方法开始:

    /**
     * Executes the given task sometime in the future.  The task
     * may execute in a new thread or in an existing pooled thread.
     *
     * If the task cannot be submitted for execution, either because this
     * executor has been shutdown or because its capacity has been reached,
     * the task is handled by the current {@code RejectedExecutionHandler}.
     *执行给定的任务,这个任务可能在一个新的线程里执行,也可能在一个已经存在的线程里执行
     *如果任务不能被提交,不管是因为executor被shutdown还是因为容量到达界限,任务都会被RejectedExecutionHandler(拒绝策略)处理。
     * @param command the task to execute
     * @throws RejectedExecutionException at discretion of
     *         {@code RejectedExecutionHandler}, if the task
     *         cannot be accepted for execution
     * @throws NullPointerException if {@code command} is null
     */
    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);
    }

首先看到这一行代码:int c = ctl.get(); ctl是什么呢?我们来看一下关于ctl的定义:

    /**
     * The main pool control state, ctl, is an atomic integer packing
     * two conceptual fields
     *   workerCount, indicating the effective number of threads
     *   runState,    indicating whether running, shutting down etc
     *
     * In order to pack them into one int, we limit workerCount to
     * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
     * billion) otherwise representable. If this is ever an issue in
     * the future, the variable can be changed to be an AtomicLong,
     * and the shift/mask constants below adjusted. But until the need
     * arises, this code is a bit faster and simpler using an int.
     *
     * The workerCount is the number of workers that have been
     * permitted to start and not permitted to stop.  The value may be
     * transiently different from the actual number of live threads,
     * for example when a ThreadFactory fails to create a thread when
     * asked, and when exiting threads are still performing
     * bookkeeping before terminating. The user-visible pool size is
     * reported as the current size of the workers set.
     *
     * 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
     *
     * Threads waiting in awaitTermination() will return when the
     * state reaches TERMINATED.
     *
     * Detecting the transition from SHUTDOWN to TIDYING is less
     * straightforward than you'd like because the queue may become
     * empty after non-empty and vice versa during SHUTDOWN state, but
     * we can only terminate if, after seeing that it is empty, we see
     * that workerCount is 0 (which sometimes entails a recheck -- see
     * below).
     */
    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; }

注释已经很清楚告诉我们ctl是workerCount和runSate的结合。我们可以看到,线程池的容量是CAPACITY(线程池中允许的最大线程数是CAPACITY),也就是2的Integer.SIZE-3次方减一。ctl用低29位表示线程池中的线程数,用剩下的高3位表示线程池的运行状态。这一点大家要理解清楚。这下面三个方法是对ctl的操作

    private static int runStateOf(int c)     { return c & ~CAPACITY; }  //获取高三位,也就是线程池的运行状态
    private static int workerCountOf(int c)  { return c & CAPACITY; }  //获取低29位,也就是线程池线程的数量
    private static int ctlOf(int rs, int wc) { return rs | wc; }  //生成ctl

理解了这个之后,我们继续回到execute方法,当一个任务被提交给线程池后,分三种情况:
1、当前线程池中线程的数量小于corePoolSize,这个时候我们直接创建一个新线程来执行提交的任务。
2、当线程池中的线程数大于corePoolSize时,如果线程池的状态是RUNNING状态,并且任务加到任务队列成功,我们仍然要再次检查一下线程池的状态,防止任务在添加到任务队列的过程中线程池被停止。如果线程池没有被停止,则调用addWorker方法尝试再创建一个线程去处理任务队列。这里只是去尝试创建,并不一定能创建成功,具体addWorker的实现我们接下来会讨论。
3、如果任务添加到任务队列失败,这个时候我们再次调用addWorker方法尝试创建一个新线程来处理当前任务,如果失败,则说明线程池被shutdown或者线程池的任务队列已经满了。
知道了一个任务被提交到线程池的处理流程之后,我们来看一下每个步骤的具体实现。首先是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;
    }

首先进入retry循环体,这个循环体的功能是去判断线程池是否可以新创建线程。首先线程池的状态如果大于SHUTDOWN状态,就不允许新创建线程(STOP状态:不再接受新任务也不处理任务队列里的任务,中断正在进行的任务;TIDYING:所有的任务被结束,workerCount被设置为0,线程状态被转变成TIDYING将会调用terminated()钩子方法;TERMINATED:线程调用完terminated()方法)。如果线程池的状态是SHUTDOWN状态,因为我们通过executor方法传进来的任务不是空,所以,这个时候会返回false,不回去了创建新的线程了。也就是说,只有线程池处于RUNNING的时候才有创建新线程的机会。然后判断当前线程数是否超过了线程池的最大容量,如果是则返回false不允许创建。然后通过CAS操作将workerCount加一,如果成功则跳出循环创建线程池,如果失败,再次判断线程池的状态和进入方法时的状态是否一致,如果不一致则重新执行retry循环体,如果一致,则重新判断线程池容量,决定是否能够创建新的线程。
如果通过以上判断,允许创建新的线程,则新创建一个Worker对象。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);
        }

        // Lock methods
        //
        // The value 0 represents the unlocked state.
        // The value 1 represents the locked state.

        protected boolean isHeldExclusively() {
            return getState() != 0;
        }

        protected boolean tryAcquire(int unused) {
            if (compareAndSetState(0, 1)) {
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }

        protected boolean tryRelease(int unused) {
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
        }

        public void lock()        { acquire(1); }
        public boolean tryLock()  { return tryAcquire(1); }
        public void unlock()      { release(1); }
        public boolean isLocked() { return isHeldExclusively(); }

        void interruptIfStarted() {
            Thread t;
            if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
                try {
                    t.interrupt();
                } catch (SecurityException ignore) {
                }
            }
        }
    }

我们先看一下Worker的构造方法:当创建Worker对象的时候,会通过我们之前设置的ThreadFactory的newThread方法来创建一个线程,并交给Worker对象持有。我们来看一下默认的线程池的实现:

public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                                  namePrefix + threadNumber.getAndIncrement(),
                                  0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }

在调用该方法的时候,会把Worker对象本身传入,我们可以看到Worker实现了Runnable接口。所以当线程启动的时候会调用的是Worker的run()方法。而Worker的run()方法调用了外部类的runWorker方法,我们看一下这个方法:

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

这个方法才是线程池处理任务的整个核心内容,进入方法后,会进入一个循环体:首先获取要执行的任务,如果当前Worker持有的任务不是空,获取的就是该任务,如果是空,就调用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;
            }
        }
    }

进入方法的时候先定义一个标识位timedOut,这个标识位用来表示从任务队列中获取任务是否超时。如果超时,说明这段时间没有新任务过来,这个线程也就是空闲的,如果当前线程数大于corePoolSize,这个线程就会被销毁。我们来看一下这个过程是怎么实现的:当设置标识位之后,进入一个循环体,来判断当前的线程池的状态,如果当前线程池的状态大于等于STOP,方法直接返回null,返回nulll是什么概念呢?我们回到runWorker方法看一下,当getTask()返回null的时候,while循环结束,执行finall语句块里的processWorkerExit方法。执行完这个方法后线程就会结束,也就是这个线程会被销毁。我们继续回到getTask()方法,当前线程池的状态大于等于STOP时,不管任务队列里是否有任务都不会获取到任务,线程会被销毁。当线程池状态是RUNNING状态的时候会继续接下来的判断,当线程池状态是SHUTDOWN的时候要去判断任务队列是否为空,如果是空就返回null,销毁线程,如果不是空继续接下来的操作。
当进行完上面的判断后,在设置一个标识位timed,这个标识位用来表示当获取任务超时后是否需要销毁线程。然后进入if ((wc > maximumPoolSize || (timed && timedOut))这个判断,如果当前线程数大于maximumPoolSize,说明线程被创建多了,这个时候要销毁线程,直接返回null。如果获取任务超时(第一次进入这个循环的时候肯定不存在这种情况,因为timedOut标识位被设置成了false),并且当前线程池里面的线程数大于1(因为要保证线程池里必须至少有一个线程)、任务队列是空的时候,返回null销毁线程。
结束以上判断的时候就要去任务队列取任务,如果timed标识位(表示当获取任务超时后是否需要销毁线程)是ture,就需要在给定时间内获取任务,不然就会返回null,如果返回null,就设置timedOut标识位为ture,表示获取任务超时,当前线程是空闲线程。等到下次循环的时候就会结束方法返回null。如果正常获取任务就讲任务返回。到此getTask()的分析结束,我们做一个小小的总结:如果线程池状态大于STOP,直接返回null销毁线程;如果当前线程池状态是SHUTDOWN并且任务队列是空,返回null销毁线程;如果不是以上两种情况,再判断线程池是否设置了空闲线程销毁,如果是的话,并且从任务队列中获取任务超时,就返回null销毁线程;如果不是就返回获取的线程。
当获取到任务之后,就去判断当前线程池是否被stop,如果是,中断当前线程,如果不是,就调用interrupted()方法取消中断标志。这一步是用来防止成功获取任务之后线程池被中断。
当做完 以上检查之后,调用beforeExecute(wt, task)方法,来执行前置操作,这个方法是个模板方法,交由子类实现。之后会执行任务的run方法,真正的执行任务。执行完任务之后会调用 afterExecute(task, thrown)方法来执行后置操作,这个方法也是模板方法。执行完之后,会再次去获取任务执行以上操作。getTask()方法返回null的时候,会调用processWorkerExit(w, completedAbruptly)方法,这个方法做了讲当前的worker对象从线程池中去除等操作(有可能还会重新创建一个线程)。有兴趣的同学可以看一下。
到此Worker分析结束,我们继续回到addWorker方法,当我们创建一个Worker对象后,讲worker对象添加到workers容器里。然后启动worker对象持有的线程。也就是用来处理任务的线程。
到此,线程池添加任务、处理任务的分析结束。

你可能感兴趣的:(Java线程池原理浅析)