源码篇-ThreadPoolExecutor

一、执行任务

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();
    // 如果运行的线程数小于核心数,添加worker
    if (workerCountOf(c) < corePoolSize) {
        // 添加worker,并将core设为true,表示是核心线程
        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);
        // 如果线程池工作线程为0,添加空的任务
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    // 走到这里,说明核心线程数用完且任务队列已满,那么启用非核心线程数,如果失败,执行拒绝策略
    else if (!addWorker(command, false))
        reject(command);
}
  • 首先用核心线程执行任务,如果核心线程已满,将任务添加到任务队列;如果队列也满了,那么用非核心线程执行任务
  • addWorker(Runnable firstTask, boolean core)第一个参数是执行的任务,第二个参数如果为true,表示用的是核心线程,false表示用的是非核心线程
  • 成员变量ctl是AtomicInteger类型,用来表示线程运行状态和线程数,高3位表示运行状态,低29位表示运行线程数
private boolean addWorker(Runnable firstTask, boolean core) {
    
    //retry用来判断是否可以添加任务,并更新线程数    
    retry:
    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // Check if queue empty only if necessary.
        // 如果线程池已经关闭且没有任务,直接返回false
        if (rs >= SHUTDOWN &&
            ! (rs == SHUTDOWN &&
               firstTask == null &&
               ! workQueue.isEmpty()))
            return false;

        for (;;) {
            // 获取工作线程数
            int wc = workerCountOf(c);
            // 如果工作线程数大于等于最大线程数,直接返回false
            if (wc >= CAPACITY ||
                wc >= (core ? corePoolSize : maximumPoolSize))
                return false;
            // 更新线程数,如果成功,跳出retry
            if (compareAndIncrementWorkerCount(c))
                break retry;
            // 走到这,说明更新线程数失败了,重新获取线程池状态
            c = ctl.get();  // Re-read ctl
            // 如果线程池状态变化了,从retry重新执行;如果线程池状态没有变化,继续for循环
            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)) {
                    // 因为线程还没启动,所以这里线程是alive,说明是不正常的
                    if (t.isAlive()) // precheck that t is startable
                        throw new IllegalThreadStateException();
                    // 将新创建的worker加入到workers集合
                    workers.add(w);
                    int s = workers.size();
                    // 更新largestPoolSize值
                    if (s > largestPoolSize)
                        largestPoolSize = s;
                    // workerAdded标记为true
                    workerAdded = true;
                }
            } finally {
                mainLock.unlock();
            }
            // 如果任务添加完成,启动线程且将workerStarted标记为true
            if (workerAdded) {
                t.start();
                workerStarted = true;
            }
        }
    } finally {
        // 如果启动线程失败,从workers删除新创建的任务,且执行tryTerminate
        if (! workerStarted)
            addWorkerFailed(w);
    }
    // 最后返回线程是否启动成功
    return workerStarted;
}
  • 首先去更新工作的线程数
  • 创建Worker对象,此时会创建Thread类型的成员变量thread,Worker对象会传入到该线程,因为Worker对象实现了Runnable方法,所以启动线程thread时,会执行Worker的run方法
Worker(Runnable firstTask) {
    setState(-1); // inhibit interrupts until runWorker
    this.firstTask = firstTask;
    this.thread = getThreadFactory().newThread(this);
}

二、执行任务

1. 发起任务执行

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

final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // allow interrupts
    // 用来表示是否正常执行任务,true表示被打断了,false表示未被打断
    boolean completedAbruptly = true;
    try {
        // 如果worker对象有传入了任务或者任务队列有任务
        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();
            }
        }
        // 被中断标记设为false
        completedAbruptly = false;
    } finally {
        // 任务完成后的操作
        processWorkerExit(w, completedAbruptly);
    }
}        
  • 首先执行传入worker对象里的任务,如果为空,则从任务队列里获取任务
  • 判断是否需要打断线程
  • 执行任务
  • 任务完成相关的操作
2. 获取任务
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.
        
        
        // 如果线程池状态关闭且任务队列为空
        // 或者
        // 线程池状态是停止
        // 那么将线程数减1,返回空任务
        if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
            decrementWorkerCount();
            return null;
        }

        // 获取线程数
        int wc = workerCountOf(c);

        // Are workers subject to culling?
        
        // 如果allowCoreThreadTimeOut为真,表示核心线程也有超时时间,一般默认为false
        // 或者
        // 工作线程数超过核心线程数
        // 那么将timed设为真,设为真的目的是为了没任务的时候,减少工作线程的数量
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

        // 如果工作线程大于最大线程数或者超时了
        // 且
        // 工作线程数大于1或者任务队列不为空
        // 那么工作线程减1
        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;
            // 如果任务为空,将超时设置为true
            timedOut = true;
        } catch (InterruptedException retry) {
            timedOut = false;
        }
    }
}
  • 这部分代码能说明线程复用,超时未获取到任务减少线程的原理
  • 如果设置了核心线程有超时时间或者线程数超过了核心线程数,那么采用带超时的方式获取任务,如果没有获取到任务,那么线程数会减1;如果不采用带超时的方式获取任务,那么一直等待,知道从任务队列里获取了任务

3. 退出任务执行

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();
    // 如果线程池状态小于STOP,说明还需要工作线程
    if (runStateLessThan(c, STOP)) {
        // 如果任务执行被中断了,保证至少还有1个工作线程在执行
        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);
    }
}
  • 从任务集合中删除当前任务
  • 尝试终止线程池
  • 如果线程池状态是小于STOP,保证有工作线程在工作
final void tryTerminate() {
    for (;;) {
        int c = ctl.get();
        // isRunning(c) 表示在运行,不能停止
        // runStateAtLeast(c, TIDYING)表示已经停止了,没必要停止
        // (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty())表示关闭但是有任务,不能停止
        if (isRunning(c) ||
            runStateAtLeast(c, TIDYING) ||
            (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
            return;
       // 如果工作线程数不等于0,停止1个空闲的工作线程,通过tryLock判断是否空闲
       if (workerCountOf(c) != 0) { // Eligible to terminate
            interruptIdleWorkers(ONLY_ONE);
            return;
        }

        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            // 走到这,说明工作线程是0了,将状态改为TIDYING
            if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
                try {
                    // 终止线程池,空方法,留给子类实现
                    terminated();
                } finally {
                    // 最后将状态改为TERMINATED,通知等待线程池终止的线程
                    ctl.set(ctlOf(TERMINATED, 0));
                    termination.signalAll();
                }
                return;
            }
        } finally {
            mainLock.unlock();
        }
        // else retry on failed CAS
    }
}
  • 如果线程池状态不在运行,且工作线程数为0,那么最终将线程池状态改为TERMINATED

三、拒绝策略

1. 直接抛异常(默认策略)
public static class AbortPolicy implements RejectedExecutionHandler {
    /**
     * Creates an {@code AbortPolicy}.
     */
    public AbortPolicy() { }

    /**
     * Always throws RejectedExecutionException.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     * @throws RejectedExecutionException always
     */
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        throw new RejectedExecutionException("Task " + r.toString() +
                                             " rejected from " +
                                             e.toString());
    }
}
2. 调用者的线程执行任务
public static class CallerRunsPolicy implements RejectedExecutionHandler {
    /**
     * Creates a {@code CallerRunsPolicy}.
     */
    public CallerRunsPolicy() { }

    /**
     * Executes task r in the caller's thread, unless the executor
     * has been shut down, in which case the task is discarded.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        if (!e.isShutdown()) {
            r.run();
        }
    }
}
3. 丢掉最早的任务
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
    /**
     * Creates a {@code DiscardOldestPolicy} for the given executor.
     */
    public DiscardOldestPolicy() { }

    /**
     * Obtains and ignores the next task that the executor
     * would otherwise execute, if one is immediately available,
     * and then retries execution of task r, unless the executor
     * is shut down, in which case task r is instead discarded.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        if (!e.isShutdown()) {
            e.getQueue().poll();
            e.execute(r);
        }
    }
}
4. 空的策略
public static class DiscardPolicy implements RejectedExecutionHandler {
    /**
     * Creates a {@code DiscardPolicy}.
     */
    public DiscardPolicy() { }

    /**
     * Does nothing, which has the effect of discarding task r.
     *
     * @param r the runnable task requested to be executed
     * @param e the executor attempting to execute this task
     */
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    }
}

四、线程工厂

static class DefaultThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    DefaultThreadFactory() {
        SecurityManager s = System.getSecurityManager();
        // 设置线程组
        group = (s != null) ? s.getThreadGroup() :
                              Thread.currentThread().getThreadGroup();
        // 设置线程前缀名
        namePrefix = "pool-" +
                      poolNumber.getAndIncrement() +
                     "-thread-";
    }

    public Thread newThread(Runnable r) {
        // 创建线程
        Thread t = new Thread(group, r,
                              namePrefix + threadNumber.getAndIncrement(),
                              0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}

五、常见四种线程池

1.newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
}
  • 可自定义线程数和线程工厂,核心线程数与最大线程数相等,这样的话线程一旦创建,就会一直运行
2.newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}
  • 只可以自定义线程工厂,核心线程数为0,最大线程数为Integer最大值
  • 相当于没有限制工作线程数,任务量大的时候,会影响机器性能
  • 空任务的时候,会有1个线程在运行
3.newSingleThreadScheduledExecutor
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1, threadFactory));
}    
  • 只创建一个工作线程,可自定义线程工厂,可以定时执行任务
4.newScheduledThreadPool
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}    
  • 可定义线程工厂和核心工作线程数,最大工作线程数为Integer.MAX_VALUE
  • 可定时执行任务
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容