线程池自定义(基础)

public class ExecutorTest {
    public static void main(String[] args) {
        //ExecutorService executorService= Executors.newSingleThreadExecutor();//单个
        //ExecutorService executorService= Executors.newFixedThreadPool(10);//固定数量
        //ExecutorService executorService= Executors.newCachedThreadPool();//可扩容
        ExecutorService executorService1=new ThreadPoolExecutor(//自定义7大参数4种参数
                4,//核心线程池
                8,//最大线程池,按CPU密集型根据服务器核数设置,io密集型2倍io线程
                3,//线程空闲3秒释放
                 TimeUnit.SECONDS,//时间单位
                 new LinkedBlockingDeque<>(3),//阻塞队列
                 Executors.defaultThreadFactory(),//线程工厂
                 new ThreadPoolExecutor.AbortPolicy()//直接抛出异常
                //new ThreadPoolExecutor.CallerRunsPolicy()//调用线程池的线程去处理
                //new ThreadPoolExecutor.DiscardOldestPolicy()//队列满了 尝试去和最早的竞争,不抛异常
                //new ThreadPoolExecutor.DiscardPolicy()//丢弃 不抛异常 我感觉抽奖之类的应该可以用
        );
        for (int i = 0; i <100 ; i++) {
            executorService1.execute(()->{
                System.out.println(
                        Thread.currentThread().getName()+"ing->ed"
                );
            });
        }
        executorService1.shutdown();
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值