ThreadPoolExecutor类

一、线程的复用方法

    创建一个Thread对象,start一次之后,等到执行完成再次start,会抛出异常,执行失败。

    但是有这样的一个模型,可以使得一个线程先后执行多个任务。即,在新线程中使用一个循环,直接调用各个Runnable的run方法,而不是把这些Runnable封装成单独线程。

    伪代码:

class PooledThread implements Runnable {
    private Queue<Runnable> taskQueue;
    private boolean stop;

    @Override
    public void run() {
        while (!stop) {
            Runnable task = taskQueue.get();
            if (task == null) Thread.sleep(10);
            else task.run();
        }
    }
}

 

二、ThreadPoolExecutor类初始化方法

    从ThreadPoolExecutor类参数最多的构造方法来看:

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.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}
corePoolSize

“核心”线程数,线程池工作时的最少线程数

maximumPoolSize

最大线程数。线程池中的线程总数不超过这个数

keepAliveTime

当线程池中的线程数量“核心”线程数时,多余线程的最长空闲时间。超时后,这些线程将被销毁

workQuueue

当线程数达到“核心线程数”时,用于暂存任务的阻塞队列。类型与大小都可以自定义

threadFactory

线程工厂,可以用来指定线程池中线程的名称等属性

handler

当阻塞队列满、线程数量也达到最大时的兜底处理策略

三、ThreadPoolExecutor类大致工作状态

    ThreadPoolExecutor线程池对象刚构建起来时,内部的worker数(一个线程的包装)为0。随着任务被提交至线程池,线程池内的动作如下:

情形

任务提交后,线程池的处理方式

worker数量未达到corePoolSize

新建一个worker用于处理被提交的任务

worker数量已经达到corePoolSize

任务进入阻塞队列workQueue中,随后被已存在的worker读取并执行

workerQueue队列中的任务达到队列容量上限

继续新建worker用于处理任务,直到worker数量达到maximumPoolSize

workerQueue队列满,且worker数量达到maximumPoolSize

提交的任务被拒绝执行,进入handle定义的兜底逻辑

    线程池本身的状态有以下五种。代码中的注释如下:

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

    线程池状态只会从上面的值变为下面的值。但中间可能跳过某些状态。

 

四、源代码

1、execute方法

    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);  //路径四
}

ctl是一个AutomicInteger对象,前三位是线程池的runState,后29位表示线程池中的worker数量。

路径一:

    如果当前worker数少于corePoolSize,则创建一个作为核心线程的worker。

    addWorker的第二个参数,表明新建worker是受corePoolSize数量限制,还是受maximumPoolSize数量限制(而不是新建的worker是核心的还是非核心的,worker没有这个标记)。

路径二:

    若路径一失败,将任务放入阻塞队列。进入队列的任务,除非遇到线程池强制关闭等特殊情况,应当被执行。

    即使将任务成功放入阻塞队列,也需要二次检查,线程池是否还在运行(增加了方法返回值的准确性),或者worker数是否已经降到0(此时新增一个worker。add一个null任务的worker是一种特殊用法,仅创建worker而不创建任务,见下面的解析)

路径三:

    若路径二仍然失败,新增一个worker。

路径四:

    若路径三仍然失败,拒绝该任务并调用兜底策略处理之。

2、addWorker方法

    addWorker方法用于新增一个worker。worker是ThreadPoolExecutor的一个内部类,是对一个运行线程的包装。

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

        // 若已经关闭了,拒绝这个任务,但是有一种同时满足三个条件的特殊情况
        if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                        firstTask == null &&
                        ! workQueue.isEmpty()))
            return false;

        for (;;) {

            // 检查当前worker数量。如果worker数量收到限制(可见core只是选择受哪一个限制,而不是给worker打上某种标记),则失败
            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 {

        //制造一个新worker
        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 {
        //如果worker没起起来,回滚各项操作
        if (! workerStarted)
            addWorkerFailed(w);
    }
    return workerStarted;
}

 

    如果线程池已经不是Running状态,则不允许新建worker。

    另外的&&条件是一种特殊情况,说明在Shutdown状态,使用null来创建一个新worker,处理workQueue(阻塞队列)中残留的任务。同时满足这三个条件,是允许创建worker的。

    此方法的第二个参数core仅仅用于容量检查,而不是给worker打上“核心”或者“非核心”的标记。worker上并没有这种标记。

    compareAndIncrementWorkerCount方法操作的是c的本体ctl,是一个AtomicInteger对象,其内部实际调用ctl.compareAndSet(c,c+1)。也就是说,从得到c的值到此处,c的值不能被修改(worker数没有增减)。

    成功则将worker计数加1,打破retry循环。

    失败则继续向下执行,得到最新的ctl值。如果runState没有变,continue小循环,再试着检查池中线程数并加1;如果变了,continue外层循环,再次获得rs。

    将任务传递给一个新建的Worker,w.thread是被worker包装的线程。检查runState,并以此判断是否把worker加入集合。第二个条件与上面的连续&&条件起的作用是一样的。t实际上是worker的包装(t和worker互相包装),执行的是runWorker方法

3、runWorker方法

    上一节的t.start,t就是Worker类中thread字段。thread字段是ThreadFactory对自己的包装,因此,t.start实际运行的是Worker.run,也就是ThreadPoolExecutor.runWorker方法。

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

    /** Delegates main run loop to outer runWorker  */
    public void run() {
        runWorker(this);
    }
    ...
    ...
}

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

    completedAbruptly用来表示,线程是否是异常退出的。

    firstTask仅仅用一次,后面的task都是调用getTask方法,从阻塞队列中取出来的。

这个if条件不容易看明白。这是需要:

    1、(runStateAtLeast(ctl.get(), STOP),或者Thread.interrupted() && runStateAtLeast(ctl.get(), STOP),满足其中一个

    2、!wt.isInterrupted()一定要满足

分析:

如果线程池已经Stop,且还没有中断的,中断这个线程;

如果线程池还没有Stop,调用Thread.interrupted清除掉这个标志位,保证线程池不中断。恰巧清除之后的当间儿,线程池Stop了,则还是中断这个线程。

beforeExecute方法和afterExecute方法可以被重写,在每个任务前后执行。

执行完成一个任务之后,completedTasks计数加1。

整个worker结束之后,收尾方法processWorkerExit被调用。其中第二个参数completedAbruptly,说明了是循环自然结束的,还是因为抛出异常导致的。

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

 

如果线程池已经Shutdown且阻塞队列空,或者线程池Stop,减去一个worker计数并返回null。这将导致上一个小节runWorker中止循环,一个worker消亡。

如果线程数超过corePoolSize,或者允许核心线程超时消亡,则将timed置为true,表示有超时返回null的需求

两组条件同时满足:

1、线程数超过maximumPoolSize,或者有超时需求且真的有超时

2、线程数大于1,或者阻塞队列已经空了

也扣减线程计数并返回null。但是为啥用的方法不一样?

 

 

timed表示是否有超时的需求,如果有的话,使用带有超时时间的poll方法;如果没有,使用无限阻塞的take方法。

看来超时撤销worker,需要执行到第二遍循环的时候,timeOut置为true,才能够做到。

当然,take阻塞调用有被中断的可能。因此有InterruptedException异常需要处理。

5、processWorkerExit方法

    worker结束后用于处理的方法。completedAbruptly为true,表示worker是异常退出的。

    假定worker正常退出时,线程池worker计数已经减1。上一节的getTask方法也确实是这样做的。

private void processWorkerExit(Worker w, boolean completedAbruptly) {
    if (completedAbruptly)
        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);
    }
}

 

统计completedTaskCount,原来是在worker结束的时候才加上的。

tryTerminate方法试图结束线程池。只要有worker结束,就将“试图”结束线程池,然而能否成功结束还要看很多条件。

若线程池还没达到Stop状态,则要小心,不能把所有的worker都给关完了。worker数量小于min(最小需要维持的数量)的话,还要给补上一个。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值