A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

2. corePoolSize vs. maxPoolSize
刚接触到这种抽象的用户可能很容易混淆这两个配置属性的区别。因此,让我们分别看一下。

2.1. corePoolSize
corePoolSize 是在不超时情况下,保持活跃的最少线程数 。它是ThreadPoolTaskExecutor的一个可配置项。但是, ThreadPoolTaskExecutor* 抽象将该值的设置委托给底层的java.util.concurrent.ThreadPoolExecutor。为验证这一点,如果我们将allowCoreThreadTimeOut设置为true,那么所有线程都可能超时,等于将corePoolSize的值设置为零。

2.2. maxPoolSize
相反,maxPoolSize定义了可以创建的最大线程数。类似地,ThreadPoolTaskExecutor的maxPoolSize属性也将其值委托给底层的java.util.concurrent.ThreadPoolExecutor。为验证这点, maxPoolSize依赖于queueCapacity,因为ThreadPoolTaskExecutor只会在其队列中的项目数超过queueCapacity*时创建一个新线程。

3. 所以,区别在哪?
corePoolSize和maxPoolSize之间的差别似乎很明显。然而,他们的行为有些微妙之处。

当我们向ThreadPoolTaskExecutor提交新任务时,如果正在运行的线程少于corePoolSize线程,即使池中有空闲线程,或者如果正在运行的线程少于maxPoolSize且由queueCapacity定义的队列已满,它也会创建一个新线程。

接下来,让我们看一些代码,以了解每个属性何时启动的示例。

4. 举例说明
首先,假设我们有一个执行新线程的方法,它来自名为startThreads的ThreadPoolTaskExecutor:

public void startThreads(ThreadPoolTaskExecutor taskExecutor, CountDownLatch countDownLatch,
  int numThreads) {
    for (int i = 0; i < numThreads; i++) {
        taskExecutor.execute(() -> {
            try {
                Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
    }
}
让我们测试ThreadPoolTaskExecutor的默认配置,它定义了一个线程的corePoolSize、一个无限制的maxPoolSize和无限制的queueCapacity。因此,我们希望无论启动多少任务,都只运行一个线程:

@Test
public void whenUsingDefaults_thenSingleThread() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(1, taskExecutor.getPoolSize());
    }
}
现在,让我们将corePoolSize更改为最多5个线程,并确保它的行为与建议中的一样。因此,无论提交给ThreadPoolTaskExecutor的任务数是多少,我们都希望启动五个线程:

@Test
public void whenCorePoolSizeFive_thenFiveThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(5, taskExecutor.getPoolSize());
    }
}
类似地,我们可以将maxPoolSize增加到10,而将corePoolSize保留为5。因此,我们希望只启动五个线程。为了更加清晰表明只有五个线程启动,因此queueCapacity仍然是无限制的:

@Test
public void whenCorePoolSizeFiveAndMaxPoolSizeTen_thenFiveThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(5, taskExecutor.getPoolSize());
    }
}
此外,我们现在将重复前面的测试,但将queueCapacity增加到10,并启动20个线程。因此,我们现在希望总共启动十个线程:

@Test
public void whenCorePoolSizeFiveAndMaxPoolSizeTenAndQueueCapacityTen_thenTenThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setQueueCapacity(10);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(20);
    this.startThreads(taskExecutor, countDownLatch, 20);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(10, taskExecutor.getPoolSize());
    }
}
同样,如果我们将queueCapactity设置为零并且只启动了10个任务,那么我们的ThreadPoolTaskExecutor中也会有10个线程。

5. 写在最后
ThreadPoolTaskExecutor是围绕java.util.concurrent.ThreadPoolExecutor的强大抽象,提供了配置corePoolSize、maxPoolSize和queueCapacity的选项。在本教程中,我们查看了corePoolSize和maxPoolSize属性,以及maxPoolSize如何与queueCapacity协同工作,从而使我们能够轻松地为任何用例创建线程池。

1 个回复

倒序浏览
有问题欢迎添加小优:DKA-2018
部分文章转载自网络
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马