jdk10 ThreadPoolExecutor阅读笔记

创建线程池常用的Executors.newFixedThreadPool Executors.newCachedThreadPool其实都是把 ThreadPoolExecutor包装了一下,返回一个ThreadPoolExecutor的对象。

构造函数

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.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
  • maximumPoolSize 线程池可以容纳的最大线程数
  • keepAliveTime 如果线程池中的线程数大于corePoolSize,超过这个时间后,才会被回收
  • unit keepAliveTime的单位,毫秒、秒什么的
  • workQueue 存放submit方法提交上的任务
  • threadFactory 创建线程的工厂类
  • handler 如果queue满了,执行的拒绝策略,比如AbortPolicy CallerRunsPolicy DiscardOldestPolicy DiscardPolicy

其中,其他三个构造函数,重载了缺失threadFactory、 handler、threadFactory和handler。默认的handler是DiscardPolicy

执行方法

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

这里面,风骚的位运算有点多。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
按照注释,ctl这个变量,用前三位来表示线程池的运行状态,用后面的29位来保存线程的数量。
private static int ctlOf(int rs, int wc) { return rs | wc; }
这个函数第一个参数是运行状态,第二个参数是工作线程数量。
ctl的初始值就是运行中RUNNING(-1 << COUNT_BITS)和当前线程数(0)或操作的结果,也就是111…29个0…;

还有两个操作这个ctl的工具方法

// 获取状态
private static int runStateOf(int c)     { return c & ~COUNT_MASK; }

// 获取运行中的线程(worker)
private static int workerCountOf(int c)  { return c & COUNT_MASK; }
  • 回过头来看代码,如果当前工作线程小于corepoolsize(workerCountOf(c) < corePoolSize),那么执行addworker操作,如果成功,执行addworker,直接返回,如果addworker返回false,那么获取ctl最新的值。
  • 如果线程池的状态为running,之后任务队列没满(offer返回true)
    • 然后再次检查,如果当前线程池状态不为running,而且remove成功,那么执行reject(reject见下文);
    • 如果再次检查通过,判断当前的工作线程是否为0,0表示没有工作线程,addWorker(null, false);启动一下
  • 如果状态不是running,或者offer失败(满了),尝试addworker一下,如果失败,执行reject

addworker

private boolean addWorker(Runnable firstTask, boolean core) {
    retry:
    for (int c = ctl.get();;) {
        // Check if queue empty only if necessary.

        if (runStateAtLeast(c, SHUTDOWN)
            && (runStateAtLeast(c, STOP)
                || firstTask != null
                || workQueue.isEmpty()))
            return false;

        for (;;) {
            if (workerCountOf(c)
                >= ((core ? corePoolSize : maximumPoolSize) & COUNT_MASK))
                return false;
            if (compareAndIncrementWorkerCount(c))
                break retry;
            c = ctl.get();  // Re-read ctl
            if (runStateAtLeast(c, SHUTDOWN))
                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 c = ctl.get();

                if (isRunning(c) ||
                    (runStateLessThan(c, STOP) && 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;
}
  • 检查线程池的状态

    • 如果状态为 STOP TIDYING TERMINATED 返回false
    • 如果状态为SHUTDOWN task不为null,,返回false
    • 如果状态SHUTDOWN 任务队列为空,返回false
    • 根据是否corepool判断当前的工作线程是否超量,如果超量,返回false
    • 如果cas增加worker的count成功,那么就走到下面的正式执行过程,否则的话,重新获取ctl的值,再次进入retry
  • 校验通过后,创建一个worker对象,入参是firsttask,如果线程创建成功了,加锁

    • 如果rs是running时,继续向下执行。如果rs为shutdown,但是firsttask为null时(也就是shutdown时,可以继续执行队列中的任务,但是不接受新任务)
    • 工作线程队列增加刚才创建的worker,workerAdded设置为true
    • 如果workerAdded为true,启动worker中的线程,workerStarted设置为true

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

局部变量task等于firsttask,fisrttask清空。如果task不为null(有firsttask),或者从队列中取到了task,加锁。
判断线程是否应该被interrupted,并执行。执行task的run方法,进入调度。
最后,清空task,防止死循环,completedTasks加一,释放锁
执行processWorkerExit,这时候要么是任务都消耗完了,要么是抛出异常了

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

加锁,将当前的worker移除worker队列
尝试tryTerminate

  • 如果状态是RUNNING 或者 SHUTDOWN
    • 如果线程不是被打断的,那么如果当前工作线程大于等于corepoolsize,直接返回,这个线程不要了
    • 执行addworker,添加新的worker(被打断或者线程小于corepoolsize)

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

先判断线程池状态,在allowCoreThreadTimeOut没设置,且corepool没有超过的时候,阻塞获取,否则,阻塞获取,让线程自然结束

你可能感兴趣的:(jdk10 ThreadPoolExecutor阅读笔记)