java ThreadPoolExecutor源码研究

2021-02-27  本文已影响0人  测试一下能打几个字

注:关键的代码的部分会有注释。

唉,好记性不如烂笔头,时间久了都忘记了,写下来以后自己忘记时复习所用

构造方法

 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;  // 线程池保存线程数量,即使任务队列为空, 新建时为0
        this.maximumPoolSize = maximumPoolSize;  //最大工作线程数量
        this.workQueue = workQueue; // 任务队列(注意设置任务队列大小例如1000 避免任务队列无限拓展内存溢出)
        this.keepAliveTime = unit.toNanos(keepAliveTime); //获取任务超时时间
        this.threadFactory = threadFactory; //线程工厂(建议使用自定义线程工厂取特殊名字,若程序运行出错jstack工具能快速找到有问题得线程)
        this.handler = handler; // 任务队列满之后处理策略(共4种,具体情况具体选择)
    }

状态属性

    /**ctl 是工作线程和运行状态合并  具体可以看此段代码得上面得注释*/
    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; }

submit 方法

   public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

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

此处注释已经详细描述了这个3if具体目的,谈谈个人理解workerCountOf(c) < corePoolSize工作线程小于默认工作线程则添加任务,会创建新工作线程获取任务执行直到不满足上述条件。isRunning(c) && workQueue.offer(command)线程池处于正在运行状态(RUNNING )且向工作队列添加任务成功,重新获取ctl的值。若此时线程不为运行状态(!=RUNNING)那么移除任务,拒绝任务。若此时工作线程数量为0那么添加空任务重新创建工作线程。!addWorker(command, false) 会新增工作线程然后直到最大线程maximumPoolSize,当然失败之后则根绝构造函数的拒绝策略决绝任务即可。

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 ||
                   //若工作线程大于了默认线程或者最大线程就会执行execute方法的第二步或者第三步,直接入队列
                    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;
    }

worker的创建以及run方法

 Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker // 运行runWorker时可中断
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);  //this使用的work本身
        }


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

获取任务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 {    
                //从工作队列中获取任务,注意这里队列使用的poll方法
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

退出工作队列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();
        
        //主要回收工作线程  正常结束则根据corePoolSize大小回收   非正常结束则添加空的任务
        int c = ctl.get();
        //检查是否为RUNNING    或 SHUTDOWN   
        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);
        }
    }

总结执行流程

线程池拓张过程(不考虑外部关闭线程池)

若小于corePoolSize 会增加工作线程 。若大于corePoolSize 会直接入队,成功就直接获取任务执行,失败(任务队列满或线程池关闭,这里指线程任务队列满)则会创建新的工作线程。

关于线程池大小边界的判断

addWorker第2个参数为true则边界corePoolSize,false为maximumPoolSize

线程池回收runWorker,processWorkerExit,completedAbruptly

 若正常结束即completedAbruptly=false,那么会根据corePoolSize 大小回收线程池 completedAbruptly为true那么新增空的工作线程
上一篇下一篇

猜你喜欢

热点阅读