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();
}
}
线程池自定义(基础)
最新推荐文章于 2023-07-09 06:36:38 发布