ThreadPoolExecutor的的核心线程回收设置allowCoreThreadTimeOut

如果你对ThreadPoolExecutor的执行还不了解,可以参考有界、无界队列对ThreadPoolExcutor执行的影响这篇文章。

在ThreadPoolExecutor类中有个allowCoreThreadTimeOut(boolean value)方法,该方法用来设置是否回收在保活时间后依然没没有任务执行核心线程。

下面通过程序来验证该参数的设置,如果你对CountDownLatch不了解,可以参考 并发工具类:等待多线程完成的CountDownLatch,和join的区别

public class PoolThreadRecycling {

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 1;
    private static final Long KEEP_ALIVE_TIME = 1L;

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            KEEP_ALIVE_TIME,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_CAPACITY),
            new ThreadPoolExecutor.CallerRunsPolicy());
    static {
        //如果设置为true,当任务执行完后,所有的线程在指定的空闲时间后,poolSize会为0
        //如果不设置,或者设置为false,那么,poolSize会保留为核心线程的数量
        executor.allowCoreThreadTimeOut(true);
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 1; i <= 10; i++) {
            executor.submit(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }

            });
        }

        System.out.println("poolSize:" + executor.getPoolSize());
        System.out.println("core:" + executor.getCorePoolSize());
        System.out.println("活跃:" + executor.getActiveCount());

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("开始检测线程池中线程数量");
        while (true) {
            Thread.sleep(1000);

            System.out.println("poolSize:" + executor.getPoolSize());
            System.out.println("core:" + executor.getCorePoolSize());
            System.out.println("活跃:" + executor.getActiveCount());
            System.out.println("======");

        }

    }
}

代码中的静态代码块中通过executor.allowCoreThreadTimeOut(true);设置回收核心线程,执行结果如下:

poolSize:9
core:5
活跃:9
开始检测线程池中线程数量
poolSize:1
core:5
活跃:0
======
poolSize:0
core:5
活跃:0
======
poolSize:0
core:5
活跃:0
======

可以看到,当设置为ture时,线程池中的任务都执行完后,线程池中的线程数量是0,因为核心线程也都被回收了。

如果将其设置为false呢,执行结果如下

poolSize:9
core:5
活跃:9
开始检测线程池中线程数量
poolSize:5
core:5
活跃:0
======
poolSize:5
core:5
活跃:0
======

线程池中的任务都执行完后,线程池中核心线程没有被回收。

该属性缺省值为false

这个参数设置成true还是false需要根据你具体的业务判断,如果该业务需要执行的次数并不多,采用多线程只是为了缩短执行的时间,那么可以设置成true,毕竟用完后很长时间才会用到,线程干耗着也是耗费资源的。但是如果是需要较高并发执行的业务,那么可以设置为false,保留着线程,避免每次都得创建线程耗费资源。

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值