美文网首页
浅析ThreadPoolExecutor

浅析ThreadPoolExecutor

作者: nanyang_lf | 来源:发表于2018-03-16 16:26 被阅读0次

先看最基本的构造函数(为了减小文章的篇幅删去了方法中的一些参数的判断)

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

corePoolSize:核心线程数量
maximumPoolSize:最大线程数
workQueue:task存放的队列
keepAliveTime:从队列中获取task的超时时间
threadFactory:这个大家应该都知道,线程工厂,可以用来指定线程名什么的
handler:拒绝task的处理方案

public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
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. 当前执行线程数小于核心线程数时直接尝试创建一个核心Worker并开始执行。如果成功则结束方法
  2. 如果上面的步骤1失败且当前线程池没有被shutdown,那么尝试将任务添加到队列中。如果此步骤成功,将重复检查线程池当前是不是被shutdown,是shutdown且将任务从队列中移除成功那么将task移交拒绝任务handler处理。或者此时的Worker是否为0,为0则继续尝试添加Worker
  3. 如果线程池为running状态但是步骤2中的将任务添加到队列中失败时,尝试直接创建一个非核心Worker,要是还是失败则移交拒绝任务的handler处理
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 {
            final ReentrantLock mainLock = this.mainLock;
            w = new Worker(firstTask);
            final Thread t = w.thread;
            if (t != null) {
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int c = ctl.get();
                    int rs = runStateOf(c);

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

如果当前线程池已经不是running状态了,此时调用addWorker方法时如果firstTask不为null或者线程池状态不是shutdown或者队列是空的将直接返回falsers == SHUTDOWN && firstTask == null && ! workQueue.isEmpty()判断主要是为了在调用了shutdown()方法后线程池可以继续将队列中没有执行完的任务执行完毕。
如果执行addWorker方法时worker的个数已经达到了最大值(2^29 - 1)或者达到了当前选择模式的最大值(corePoolSize或者maximumPoolSize)也将直接返回false
上面的判断都通过检查之后,将进行到添加worker的阶段(线程安全)。

public void run() {
            runWorker(this);
        }

workerrun方法直接调用了下面的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);
        }
    }

如果线程池被调用了stop或者terminated方法且当前执行任务线程没有被interrupted那么将调用当前线程的interruupt方法,中断线程执行。再次注意,被shutdown的线程池会继续将队列中的任务完成。
这里有两个方法可能会对我们有用:beforeExecuteafterExecute
beforeExecute:如果我们想要在任务执行之前完成某些操作如日志记录就可以实现一个ThreadPoolExecutor然后重写beforeExecute方法,但是我们应该注意一下不要在这个方法里面写出可能抛出异常的语句(如果抛出异常是你想要的当我没说),因为如果抛出了异常将不会执行继续任务。
afterExecute:这个方法是在任务完成之后一定会执行的方法,不管你的任务代码中有没有抛出异常,可以完成一些关闭或者修复现场的操作。

看上面的`runWorker`方法中`while`循环条件中有一个`getTask`方法下面将分析它:
private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?
        retry:
        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;
            }

            boolean timed;      // Are workers subject to culling?

            for (;;) {
                int wc = workerCountOf(c);
                timed = allowCoreThreadTimeOut || wc > corePoolSize;

                if (wc <= maximumPoolSize && ! (timedOut && timed))
                    break;
                if (compareAndDecrementWorkerCount(c))
                    return null;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }

            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

上面timed变量表示:是否已经到时候将当前worker对象从池中回收,成立的情况为timed = allowCoreThreadTimeOut || wc > corePoolSize; 线程池设置了允许核心线程也可以超时(默认为false即核心线程不超时),或者当前woker不是核心线程所对应的(即当前池中所有worker数量已经超过了corePoolSize)
allowCoreThreadTimeOut如果设置为true那么只要当前没有任务Worker对象就会被回收
timeOut:执行过一次从workQueue中获取task后将被置为超时
所以getTask这个方法如果当前timed为false就会阻塞直到获取到一个task。

相关文章

网友评论

      本文标题:浅析ThreadPoolExecutor

      本文链接:https://www.haomeiwen.com/subject/uwtrqftx.html