ThreadPool

线程池

为什么使用线程池

  • 创建/销毁线程需要消耗系统资源,线程池可以复用已创建的线程
  • 并发线程数量过多,可能会导致资源消耗过多,从而造成服务器崩溃,线程池可以统一管理线程,控制并发的数量(主要原因);

原理

关系图

  • ThreadPoolExecutor

    四种构造方法

    // 五个参数的构造函数
    public ThreadPoolExecutor(int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue)

    // 六个参数的构造函数-1
    public ThreadPoolExecutor(int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue,
    ThreadFactory threadFactory)

    // 六个参数的构造函数-2
    public ThreadPoolExecutor(int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue,
    RejectedExecutionHandler handler)

    // 七个参数的构造函数
    public ThreadPoolExecutor(int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue,
    ThreadFactory threadFactory,
    RejectedExecutionHandler handler)
    • int corePoolSize:该线程池中核心线程数最大值

      核心线程:线程池中有两类线程,核心线程和非核心线程。核心线程默认情况下会一直存在于线程池中,即使这个核心线程什么都不干。

    • int maximumPoolSize:该线程池中线程总数最大值

    • long keepAliveTime非核心线程闲置超时时长

      非核心线程如果处于闲置状态超过该值,就会被销毁。如果设置allowCoreThreadTimeOut(true),则会也作用于核心线程。

    • TimeUnit unit:keepAliveTime的单位。

      TimeUnit是一个枚举类型 ,包括以下属性:

      NANOSECONDS : 1微毫秒 = 1微秒 / 1000

      MICROSECONDS : 1微秒 = 1毫秒 / 1000

      MILLISECONDS : 1毫秒 = 1秒 /1000

      SECONDS : 秒

      MINUTES : 分

      HOURS : 小时

      DAYS : 天

    • BlockingQueue workQueue:阻塞队列,维护着等待执行的Runnable任务对象

    • ThreadFactory threadFactory:创建线程的工厂,用于批量创建线程,统一在创建线程时设置一些参数,如是否守护线程、线程的优先级等。如果不指定,会新建一个默认的线程工厂。

      /**
      * The default thread factory
      */
      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;
      }
      }
    • RejectedExecutionHandler handler拒绝处理策略,线程数量大于最大线程数就会采用拒绝处理策略,四种:

      ThreadPoolExecutor.AbortPolicy默认拒绝处理策略,丢弃任务并抛出RejectedExecutionException异常;

      ThreadPoolExecutor.DiscardPolicy:丢弃新来的任务,但是不抛出异常;

      ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列头部(最旧的)的任务,然后重新尝试执行程序(如果再次失败,重复此过程);

      ThreadPoolExecutor.CallerRunsPolicy:由调用线程处理该任务;

    线程池状态

    线程池本身有一个调度线程,这个线程就是用于管理布控整个线程池里的各种任务和事务,例如创建线程、销毁线程、任务队列管理、线程队列管理等等,故线程池也有自己的状态。

    /**
    * The main pool control state, ctl, is an atomic integer packing
    * two conceptual fields
    * workerCount, indicating the effective number of threads
    * runState, indicating whether running, shutting down etc
    *
    * In order to pack them into one int, we limit workerCount to
    * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
    * billion) otherwise representable. If this is ever an issue in
    * the future, the variable can be changed to be an AtomicLong,
    * and the shift/mask constants below adjusted. But until the need
    * arises, this code is a bit faster and simpler using an int.
    *
    * The workerCount is the number of workers that have been
    * permitted to start and not permitted to stop. The value may be
    * transiently different from the actual number of live threads,
    * for example when a ThreadFactory fails to create a thread when
    * asked, and when exiting threads are still performing
    * bookkeeping before terminating. The user-visible pool size is
    * reported as the current size of the workers set.
    *
    * The runState provides the main lifecycle control, taking on values:
    *
    * RUNNING: Accept new tasks and process queued tasks
    * SHUTDOWN: Don't accept new tasks, but process queued tasks
    * STOP: Don't accept new tasks, don't process queued tasks,
    * and interrupt in-progress tasks
    * TIDYING: All tasks have terminated, workerCount is zero,
    * the thread transitioning to state TIDYING
    * will run the terminated() hook method
    * TERMINATED: terminated() has completed
    *
    * The numerical order among these values matters, to allow
    * ordered comparisons. The runState monotonically increases over
    * time, but need not hit each state. The transitions are:
    *
    * RUNNING -> SHUTDOWN
    * On invocation of shutdown(), perhaps implicitly in finalize()
    * (RUNNING or SHUTDOWN) -> STOP
    * On invocation of shutdownNow()
    * SHUTDOWN -> TIDYING
    * When both queue and pool are empty
    * STOP -> TIDYING
    * When pool is empty
    * TIDYING -> TERMINATED
    * When the terminated() hook method has completed
    *
    * Threads waiting in awaitTermination() will return when the
    * state reaches TERMINATED.
    *
    * Detecting the transition from SHUTDOWN to TIDYING is less
    * straightforward than you'd like because the queue may become
    * empty after non-empty and vice versa during SHUTDOWN state, but
    * we can only terminate if, after seeing that it is empty, we see
    * that workerCount is 0 (which sometimes entails a recheck -- see
    * below).
    */
    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;

    • 线程池创建后处于RUNNING状态;

    • 调用shutdown()方法后处于SHUTDOWN状态,线程池不能接受新的任务,清除一些空闲worker,会等待阻塞队列的任务完成;(SHUTDOWN:不接受新任务,但处理排队任务)

    • 调用shutdownNow()方法后处于STOP状态,线程池不能接受新的任务,中断所有线程,阻塞队列中没有被执行的任务全部丢弃。此时,poolsize=0,阻塞队列的size也为0;(STOP:不接受新任务,也不处理排队任务,并中断正在执行的任务)

    • 当所有的任务已终止,ctl记录的“任务数量”为0,线程池会变为TIDYING状态。接着会执行terminated()钩子方法;

      ThreadPoolExecutor中有一个控制状态的属性叫ctl,它是一个AtomicInteger类型的变量。

    • 线程池处在TIDYING状态时,执行完terminated()方法之后,就会由 TIDYING -> TERMINATED, 线程池被设置为TERMINATED状态;

    线程池的任务处理流程

    // JDK 1.8
    // 执行命令,其中命令(下面称任务)对象是Runnable的实例
    public void execute(Runnable command) {
    // 判断命令(任务)对象非空
    if (command == null)
    throw new NullPointerException();
    // 获取ctl的值
    int c = ctl.get();
    // 判断如果当前工作线程数小于核心线程数,则创建新的核心线程并且执行传入的任务
    if (workerCountOf(c) < corePoolSize) {
    if (addWorker(command, true))
    // 如果创建新的核心线程成功则直接返回
    return;
    // 这里说明创建核心线程失败,需要更新ctl的临时变量c
    c = ctl.get();
    }
    // 如果不小于corePoolSize,则将任务添加到workQueue队列。
    // 判断线程池是否处于运行中状态,同时尝试用非阻塞方法向任务队列放入任务(放入任务失败返回false)
    if (isRunning(c) && workQueue.offer(command)) {
    int recheck = ctl.get();
    // 这里是向任务队列投放任务成功,对线程池的运行中状态做二次检查
    // 如果线程池二次检查状态是非运行中状态,则从任务队列移除当前的任务调用拒绝策略处理之(也就是移除前面成功入队的任务实例)
    if (! isRunning(recheck) && remove(command))
    // 调用拒绝策略处理任务 - 返回
    reject(command);
    // 线程池处于running状态,但是没有线程,则创建线程
    // 如果当前工作线程数量为0,则创建一个非核心线程并且传入的任务对象为null - 返回
    // 也就是创建的非核心线程不会马上运行,而是等待获取任务队列的任务去执行
    // 如果前工作线程数量不为0,原来应该是最后的else分支,但是可以什么也不做,因为任务已经成功入队列,总会有合适的时机分配其他空闲线程去执行它
    else if (workerCountOf(recheck) == 0)
    addWorker(null, false);
    }
    // 走到这里说明有以下的前提:
    // 0、线程池中的工作线程总数已经大于等于corePoolSize(简单来说就是核心线程已经全部懒创建完毕)
    // 1、线程池可能不是RUNNING状态
    // 2、线程池可能是RUNNING状态同时任务队列已经满了
    // 如果放入workQueue失败,则创建非核心线程执行任务,
    // 如果这时创建非核心线程失败(当前线程总数不小于maximumPoolSize时),就会执行拒绝策略。
    else if (!addWorker(command, false))
    // 调用拒绝策略处理任务 - 返回
    reject(command);
    }

    为什么要二次检查线程池的状态?

    在多线程的环境下,线程池的状态是时刻发生变化的。很有可能刚获取线程池状态后线程池状态就改变了。判断是否将command加入workqueue是线程池之前的状态。倘若没有二次检查,万一线程池处于非RUNNING状态(在多线程环境下很有可能发生),那么command永远不会执行。

    整体流程:

    1. 线程总数量小于corePoolSize,无论线程是否空闲,都会直接创建核心线程执行任务(注意,这一步需要获得全局锁);
    2. 线程总数量大于等于corePoolSize,判断线程池是否处于运行中状态,同时尝试用非阻塞方法向任务队列放入任务,这里会二次检查线程池运行状态,如果当前工作线程数量为0,则创建一个非核心线程并且传入的任务对象为null;
    3. 如果缓存队列满了,则会尝试创建非核心线程传入任务实例执行(注意这一步需要获得全局锁);
    4. 如果创建非核心线程失败,此时需要拒绝执行任务,调用拒绝策略处理任务;

    ThreadPoolExecutor如何做到线程复用的?

    /**
    * Checks if a new worker can be added with respect to current
    * pool state and the given bound (either core or maximum). If so,
    * the worker count is adjusted accordingly, and, if possible, a
    * new worker is created and started, running firstTask as its
    * first task. This method returns false if the pool is stopped or
    * eligible to shut down. It also returns false if the thread
    * factory fails to create a thread when asked. If the thread
    * creation fails, either due to the thread factory returning
    * null, or due to an exception (typically OutOfMemoryError in
    * Thread.start()), we roll back cleanly.
    *
    * @param firstTask the task the new thread should run first (or
    * null if none). Workers are created with an initial first task
    * (in method execute()) to bypass queuing when there are fewer
    * than corePoolSize threads (in which case we always start one),
    * or when the queue is full (in which case we must bypass queue).
    * Initially idle threads are usually created via
    * prestartCoreThread or to replace other dying workers.
    *
    * @param core if true use corePoolSize as bound, else
    * maximumPoolSize. (A boolean indicator is used here rather than a
    * value to ensure reads of fresh values after checking other pool
    * state).
    * @return true if successful
    */
    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 {
    // 创建一个worker对象
    w = new Worker(firstTask);
    // 实例化一个Thread对象
    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类部分源码:

    // Worker类部分源码
    private final class Worker extends AbstractQueuedSynchronizer implements Runnable{
    final Thread thread;
    Runnable firstTask;

    Worker(Runnable firstTask) {
    setState(-1); // inhibit interrupts until runWorker
    this.firstTask = firstTask;
    this.thread = getThreadFactory().newThread(this);
    }

    public void run() {
    runWorker(this);
    }
    //其余代码略...
    }

    Worker类实现了Runnable接口,所以Worker也是一个线程任务。在构造方法中,创建了一个线程,线程的任务就是自己。故addWorker方法调用addWorker方法源码下半部分中的第4步t.start,会触发Worker类的run方法被JVM调用。

  1. 常见的四种线程池

    1)newCachedThreadPool:它是一个可以无限扩大的线程池

    public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
    60L, TimeUnit.SECONDS,
    new SynchronousQueue<Runnable>());
    }

    它比较适合处理执行时间比较短的任务;

    corePoolSize为0,maxPoolSize为无限大,意味着线程数量可以无限大,keepAliveTime为60S,意味着线程空闲时间超过60S就会被回收;

    采用SynchronousQueue装等待的任务,这个阻塞队列没有存储空间,这意味着只要有请求到来,就必须要找到一条工作线程处理他,如果当前没有空闲的线程,那么就会再创建一条新的线程;

    存在的问题:无界线程池,具有自动回收多余线程的功能。弊端在于第二个参数maxPoolSize被设置为Integer.MAX_VALUE,这可能会创建数量非常多的线程,甚至导致OOM。

    2)newFixedThreadPool:它是一种固定大小的线程池

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

    corePoolSize和maxPoolSize都为用户设定的线程数量nThreads;

    keepAliveTime为0,意味着一旦有多余的空闲线程,就会被立即停止掉;但这里keepAliveTime无效;

    阻塞队列采用了LinkedBlockingQueue,它是一个无界队列,因此永远不可能拒绝任务;故如果核心线程空闲,则交给核心线程处理;如果核心线程不空闲,则入列等待,直到核心线程空闲。

    存在的问题:由于传入的LinkedBlockingQueue是没有容量上限的,所以当请求数越来越多,并且无法及时处理完毕的时候,即请求堆积的时候,会容易造成占用大量的内存,可能会导致OOM。

    与CachedThreadPool的区别

    • 因为 corePoolSize == maximumPoolSize ,所以FixedThreadPool只会创建核心线程。 而CachedThreadPool因为corePoolSize=0,所以只会创建非核心线程。
    • 在 getTask() 方法,如果队列里没有任务可取,线程会一直阻塞在 LinkedBlockingQueue.take() ,线程不会被回收。 CachedThreadPool会在60s后收回。
    • 由于线程不会被回收,会一直卡在阻塞,所以没有任务的情况下, FixedThreadPool占用资源更多
    • 都几乎不会触发拒绝策略,但是原理不同。FixedThreadPool是因为阻塞队列可以很大(最大为Integer最大值),故几乎不会触发拒绝策略;CachedThreadPool是因为线程池很大(最大为Integer最大值),几乎不会导致线程数量大于最大线程数,故几乎不会触发拒绝策略。

    3)newSingleThreadExecutor:有且仅有一个核心线程

    public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
    (new ThreadPoolExecutor(1, 1,
    0L, TimeUnit.MILLISECONDS,
    new LinkedBlockingQueue<Runnable>()));
    }

    有且仅有一个核心线程(corePoolSize = maximumPoolSize = 1),使用了LinkedBlockingQueue(容量很大),所以不会创建非核心线程。所有任务按照先来先执行的顺序执行。

    存在的问题:由于传入的LinkedBlockingQueue是没有容量上限的,所以如果这个唯一的线程不空闲,那么新来的任务会堆积在任务队列里等待执行,会容易造成占用大量的内存,可能会导致OOM。

    4)newScheduledThreadPool:可调度的线程池

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
    }

    //ScheduledThreadPoolExecutor():
    public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE,
    DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
    new DelayedWorkQueue());
    }

    创建一个定长线程池,支持定时及周期性任务执行。

  2. 线程池里的线程数量设定为多少比较合适?

    • CPU密集型(加密,计算hash等):最佳线程数为CPU核心数的1-2倍左右;
  • 耗时IO型(读写数据库、文件、网络读写等):最佳线程数一般会大于CPU核心数很多倍,以JVM线程监控显示繁忙情况为依据,保证线程空闲可以衔接上。

参考Brain Goetz推荐的计算方法;

线程数 = CPU核心数 * (1 + 平均等待时间 / 平均工作时间)

或者进行压测,根据压测结果确定线程数;

阻塞队列

场景

BlockingQueue一般用于生产者-消费者模式,生产者是往队列里添加元素的线程,消费者是从队列里拿元素的线程。BlockingQueue就是存放元素的容器

操作方法

方法\处理方式 抛出异常 返回特殊值 一直阻塞 超时退出
插入方法 add(e) offer(e) put(e) offer(e,time,unit)
移除方法 remove() poll() take() poll(time,unit)
检查方法 element() peek() - -
  • 抛出异常:如果试图的操作无法立即执行,抛异常。当阻塞队列满时候,再往队列里插入元素,会抛出IllegalStateException(“Queue full”)异常。当队列为空时,从队列里获取元素时会抛出NoSuchElementException异常 。
  • 返回特殊值:如果试图的操作无法立即执行,返回一个特殊值,通常是true / false。
  • 一直阻塞:如果试图的操作无法立即执行,则一直阻塞或者响应中断。
  • 超时退出:如果试图的操作无法立即执行,该方法调用将会发生阻塞,直到能够执行,但等待时间不会超过给定值。返回一个特定值以告知该操作是否成功,通常是 true / false。

注意之处

  • 不能往阻塞队列中插入null,会抛出空指针异常。
  • 可以访问阻塞队列中的任意元素,调用remove(o)可以将队列之中的特定对象移除,但并不高效,尽量避免使用。
  1. 实现类

    1)ArrayBlockingQueue

    有界阻塞队列,内部结构是数组,故具有数组的特性。

    public ArrayBlockingQueue(int capacity, boolean fair){
    //..省略代码
    }

    可以初始化队列大小, 且一旦初始化不能改变。构造方法中的fair表示控制对象的内部锁是否采用公平锁,默认是非公平锁

    2)LinkedBlockingQueue

    有界阻塞队列,内部结构是链表,具有链表的特性。默认队列的大小是Integer.MAX_VALUE,也可以指定大小。此队列按照先进先出的原则对元素进行排序。

    3)DelayQueue

    DelayQueue是一个没有大小限制的队列,因此往队列中插入数据的操作(生产者)永远不会被阻塞,而只有获取数据的操作(消费者)才会被阻塞。

    注入其中的元素必须实现 java.util.concurrent.Delayed 接口。该队列中的元素只有当其指定的延迟时间到了,才能够从队列中获取到该元素。

    4)PriorityBlockingQueue

    基于优先级的无界阻塞队列(优先级的判断通过构造函数传入的Compator对象来决定),内部控制线程同步的锁采用的是公平锁。

    PriorityBlockingQueue不会阻塞数据生产者(因为队列是无界的),而只会在没有可消费的数据时,阻塞数据的消费者。因此使用的时候要特别注意,生产者生产数据的速度绝对不能快于消费者消费数据的速度,否则时间一长,会最终耗尽所有的可用堆内存空间。对于使用默认大小的LinkedBlockingQueue也是一样的。

    5)SynchronousQueue

    这个队列比较特殊,没有任何内部容量,甚至连一个队列的容量都没有。并且每个put必须等待一个take,反之亦然。

    需要区别容量为1的ArrayBlockingQueue、LinkedBlockingQueue。

    以下方法的返回值,可以帮助理解这个队列:

    • iterator() 永远返回空,因为里面没有东西;
    • peek() 永远返回null;
    • put() 往queue放进去一个element以后就一直wait直到有其他thread进来把这个element取走;
    • offer() 往queue里放一个element后立即返回,如果碰巧这个element被另一个thread取走了,offer方法返回true,认为offer成功;否则返回false;
    • take() 取出并且remove掉queue里的element,取不到东西他会一直等;
    • poll() 取出并且remove掉queue里的element,只有到碰巧另外一个线程正在往queue里offer数据或者put数据的时候,该方法才会取到东西。否则立即返回null;
    • isEmpty() 永远返回true;
    • remove()&removeAll() 永远返回false;

    源码分析

    • 构造器

      对同一个锁(lock)初始化了两个监视器,分别是notEmpty和notFull。这两个监视器的作用目前可以简单理解为标记分组,当该线程是put操作时,给他加上监视器notFull,标记这个线程是一个生产者;当线程是take操作时,给他加上监视器notEmpty,标记这个线程是消费者。

      //数据元素数组
      final Object[] items;
      //下一个待取出元素索引
      int takeIndex;
      //下一个待添加元素索引
      int putIndex;
      //元素个数
      int count;
      //内部锁
      final ReentrantLock lock;
      //消费者监视器
      private final Condition notEmpty;
      //生产者监视器
      private final Condition notFull;

      public ArrayBlockingQueue(int capacity, boolean fair) {
      //..省略其他代码
      lock = new ReentrantLock(fair);
      notEmpty = lock.newCondition();
      notFull = lock.newCondition();
      }
    • put源码

      public void put(E e) throws InterruptedException {
      checkNotNull(e);
      final ReentrantLock lock = this.lock;
      // 1.自旋拿锁
      lock.lockInterruptibly();
      try {
      // 2.判断队列是否满了
      while (count == items.length)
      // 2.1如果满了,阻塞该线程,并标记为notFull线程,
      // 等待notFull的唤醒,唤醒之后继续执行while循环。
      notFull.await();
      // 3.如果没有满,则进入队列
      enqueue(e);
      } finally {
      lock.unlock();
      }
      }
      private void enqueue(E x) {
      // assert lock.getHoldCount() == 1;
      // assert items[putIndex] == null;
      final Object[] items = this.items;
      items[putIndex] = x;
      if (++putIndex == items.length)
      putIndex = 0;
      count++;
      // 4 唤醒一个等待的线程
      notEmpty.signal();
      }
      1. 所有执行put操作的线程竞争lock锁,拿到了lock锁的线程进入下一步,没有拿到lock锁的线程自旋竞争锁。
      2. 判断阻塞队列是否满了,如果满了,则调用await方法阻塞这个线程,并标记为notFull(生产者)线程,同时释放lock锁,等待被消费者线程唤醒。
      3. 如果没有满,则调用enqueue方法将元素put进阻塞队列。注意这一步的线程还有一种情况是第二步中阻塞的线程被唤醒且又拿到了lock锁的线程。
      4. 唤醒一个标记为notEmpty(消费者)的线程。
    • take源码

      public E take() throws InterruptedException {
      final ReentrantLock lock = this.lock;
      lock.lockInterruptibly();
      try {
      while (count == 0)
      notEmpty.await();
      return dequeue();
      } finally {
      lock.unlock();
      }
      }
      private E dequeue() {
      // assert lock.getHoldCount() == 1;
      // assert items[takeIndex] != null;
      final Object[] items = this.items;
      @SuppressWarnings("unchecked")
      E x = (E) items[takeIndex];
      items[takeIndex] = null;
      if (++takeIndex == items.length)
      takeIndex = 0;
      count--;
      if (itrs != null)
      itrs.elementDequeued();
      notFull.signal();
      return x;
      }

      take操作和put操作的流程是类似的,总结一下take操作的流程:

      1. 所有执行take操作的线程竞争lock锁,拿到了lock锁的线程进入下一步,没有拿到lock锁的线程自旋竞争锁。
      2. 判断阻塞队列是否为空,如果是空,则调用await方法阻塞这个线程,并标记为notEmpty(消费者)线程,同时释放lock锁,等待被生产者线程唤醒。
      3. 如果没有空,则调用dequeue方法。注意这一步的线程还有一种情况是第二步中阻塞的线程被唤醒且又拿到了lock锁的线程。
      4. 唤醒一个标记为notFull(生产者)的线程。
  2. 应用场景

    • 生产者-消费者模式
    • 线程池中

常见问题

  1. submit和execute区别

    • 参数不一样
    void execute(Runnable command);
    Future<T> submit(Callable<T> task);
    Future<T> submit(Runnable task, T result);
    Future<?> submit(Runnable task);
    • execute()在Executor接口中定义的,在ThreadPoolExecutor中实现的;

    • execute会在运行期直接抛出异常,submit在调用Future.get时才会抛出异常;

  2. 线程池如何保证当前线程获取池内的worker时不产生争用

    worker实现了AQS,通过volatile修饰的state

    // Lock methods
    //
    // The value 0 represents the unlocked state.
    // The value 1 represents the locked state.

    protected boolean isHeldExclusively() {
    return getState() != 0;
    }
Author: Jiayi Yang
Link: https://jiayiy.github.io/2020/08/05/ThreadPool/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.