ThreadPoolExecutor参数测试--记代码

本文深入解析线程池核心参数配置,包括核心线程数、最大线程数、工作队列容量及拒绝策略。通过示例代码展示线程池在高并发场景下的工作原理与任务调度机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程池应该已经有很多人总结了,记录一下简单的参数验证:

public class ThreadPool {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 60,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(3), new CustomizableThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

        for (int i=0; i<20; i++) {
            final int id = i;
            System.out.println("threadPool alive Count si : " + threadPoolExecutor.getActiveCount() );
            threadPoolExecutor.execute(new Thread(()->{
                try {
                    System.out.println("Current Tread name is : " + Thread.currentThread().getName() + "/ ThreadNum is " + id) ;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }));
        }
    }
}

运行截图:

参数说明:

1.核心线程数5,当存活线程为5,

2.之后来的任务塞队列,这里设置大小3方便查看,

3.塞满队列,开始new新的线程,直到超过最大线程数;

4.拒绝策略,这里默认用的是超过最大抛异常;

----to be continued

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值