ThreadPoolExecutor学习笔记

线程池状态:

高3位表示"线程池状态"
低29位表示"线程池中的任务数量"

public class ThreadPoolExecutor extends AbstractExecutorService {
    
    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; }
public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {   // 如果工作线程数 < corePoolSize,就new 一个新的线程
            if (addWorker(command, true))
                return;
            c = ctl.get();                  // 如果线程new 失败了,说明不满足runState 和workerCount 的要求,有并发的操作,重新获取一次标志位
        }
        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))             //如果队列也添加不了,就new thread直到maxPoolSize,如果失败就执行拒绝策略
            reject(command);
    }

Worker

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);
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 11,324评论 4 56
  • 线程池的概念和定义 在服务器端的业务应用开发中,Web服务器(诸如Tomcat、Jetty)需要接受并处理http...
    dtdh阅读 850评论 0 1
  • 线程池中有一定数量的工作线程,工作线程会循环从任务队列中获取任务,并执行这个任务。那么怎么去停止这些工作线程呢?这...
    wo883721阅读 1,650评论 0 14
  • 撩开 夜的窗帘 我看到月亮的眼睛 闪动的眸 似清澈的泉水 融化了天上的星星 夜那么静 仿佛 能听到秋天的脚步 在季...
    桂花上酸菜阅读 513评论 17 29
  • 不论朝夕,只争你我 今年的中秋要比往年冷一些,从大巴上下车已经是深夜了,我急着赶回家,紧了紧衣袖,...
    汤城阅读 396评论 0 0