★
一 概念
Executors和arrays,collections一样,是一个工具类。,那么底层核心在ThreadPoolExecutor上面。
有5种实现,重点3种:通过executors获得。
关于线程池实现类的 重要参数:常用五个参数,实际7个参数。
1.corePoolSize:常驻线程数量,
2.maximumPoolSize:能够容纳同时执行的最大线程数(扩容机制)
3.keepAliveTime: 多余的空闲线程的存活时间,当空闲线程时间达到指定值,就会进行摧毁,直到线程数到corePoolSize
4.unit: keepAliveTime的单位
5.workQueue:任务队列,被提交但尚未被执行的任务。
6.threadFactory: 生成线程池种工作线程的线程工厂,用于创建线程
7.handler:拒绝策略,当阻塞队列也满了,是否扩容?扩容到了极限呢?开始拒绝请求,handler启动。默认自带四种策略。
- AbortPolicy(默认):直接抛出RejectedExecutionException异常 阻止系统正常运行。
- CallerRunsPolicy: 回退到 调用者所在的线程。
- DiscardOldestPolicy:抛弃队列中等待最久的任务,然后把当前任务加入队列中尝试再次提交当前任务。
- DiscardPolicy: 直接丢弃任务,不做任何处理(如果任务可以丢失,那么就是最好的处理方案)
二 实现类
1.fixedThreadPool
固定装载的线程数量。
public static void main(String[] args) {
ExecutorService threadPool= Executors.newFixedThreadPool(5); //一池5个线程
//模拟10个用户 办理业务
for(int i=1;i<=20;i++){
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
}
}
executors创建使用的参数:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
对于上方的前五个参数可知,corepoolsize和maximumPoolSize都是固定的,也就是我们给的参数,5个。
里面使用了一个LinkedBlockQueue,有最大线程数量,如果来的请求了超过了线程池的最大数量,那么就会放入阻塞队列
工作流程:
1.当前运行的线程数少于corePoolSize,则创建按新的线程来执行任务。
2.在线程池完成预热之后(当前运行的线程数等于corePoolSize),将任务加入LinkedBlockingQueue中
无界队列LinkedBlockingQueue的影响:(队列的容量为Integer.Max_value)
1.当线程池中的线程数达到corePoolSize的时候,多余的线程将放入队列中
2.使用无界队列,则maximumPoolSize将是一个无效参数。则KeepAliveTime也是一个无效参数
3.使用无界队列,不会拒绝任务。
2.singleThreadExecutor
特点:一池一线程
ExecutorService threadPool= Executors.newSingleThreadExecutor();
try{
for(int i=1;i<=20;i++){
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
}
executors工具类 创建线程池:
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
同样使用无限队列LinkedBlockingQueue来装载多余线程,那么只能允许一个线程,为什么还需要线程池呢?
一个线程,保证了线程的安全,将多个线程的并发阻塞成一个。
3.CachedThreadPool
特点:一池N个处理线程(线程数量未知)
ExecutorService threadPool= Executors.newCachedThreadPool();
try{
for(int i=1;i<=20;i++){
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
}
executors工具类的内部:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
它的corePoolSize设置成为0,而maximumPoolSize设置成为Integer.max_value,第三个参数为60,这个是闲置线程死亡时间,第四个参数为时间的单位,秒,所以当一个线程闲置时间超过60s,就会销毁。
它的等待队列:SynchronousQueue是没有容量的队列,所以不会有线程超过maximumPoolSize进入等待队列,这个时候你不得不感叹jdk自带的线程池实现类,都好极端。
4、ScheduledThreadPool
创建一个可定期或者延时执行任务的定长线程池,支持定时及周期性任务执行。
ExecutorService threadPool= Executors.newScheduledThreadPool(5);
Runnable r1=()-> System.out.println("线程名称:"+Thread.currentThread().getName()+" ,执行:3秒后执行");
((ScheduledExecutorService) threadPool).schedule(r1,3, TimeUnit.SECONDS);
try{
for(int i=1;i<=10;i++){
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
}
executors底层实现:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
使用延迟队列作为阻塞队列,corePool中线程获取队列中的任务方式不同,完成了线程池定时任务的功能。
总结:
工作中,不要使用上面四个,要使用自定义的!
在阿里巴巴开发手册中规定,不要使用Executors创建线程池,因为一个队列长度长达21亿,对于服务器来说,是绷不住的,所以我们要自己取实现。
参数如何配置合理?
1.与硬件有关系。cpu密集型还是IO密集型。