*流程图参考博客:https://blog.csdn.net/u010723709/article/details/50372322
一、ThreadPoolExecutor构造
ThreadPoolExecutor类继承AbstractExecutorService类
AbstractExecutorService类实现ExecutorService接口
ExecutorService类继承Executor类
二、ThreadPoolExecutor类中的4种构造方法
1). public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
2). public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory)
3).public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
4). public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
三、ThreadPoolExecutor类中的构造方法的参数解释
1).int corePoolSize【核心线程数最大值】
a.线程池新建线程的时候,会先去比较是否大于核心线程数,小于的情况下创建核心线程,大于的情况下则创建非核心线程数,核心线程数可以通过设置allowCoreThreadTimeOut=true,long keepAliveTime=time【超时时间】来销毁空闲的核心线程,如果不设置该值,则一直存在于线程池中。
2).int maximumPoolSize【线程总数最大值,即核心线程数+非核心线程数】
3).long keepAliveTime【非核心线程闲置超时时间】
a.一个非核心线程闲置状态下超过keepAliveTime,则销毁该线程,控制核心线程时,需要同时设置allowCoreThreadTimeOut的值为true。
4).TimeUnit unit【keepAliveTime的单位,枚举类型】
5).BlockingQueue<Runnable> workQueue【线程池的任务队列】
a.所有核心线程无闲置的情况下,新添加的任务会放入该队列等待处理,如果队列满了,就创建非核心线程执行任务
b.常用的workQueue类型
①.SynchronousQueue:接收任务之后直接提交给线程处理,如果线程都在工作,则直接创建线程,因为可以无限创建线程,所以该队列的maximumPoolSize指定为Integer.MZX_VALUE。
②.LinkedBlockingQueue:接收任务之后判断当前线程是否小于核心线程数,如果少于则创建核心线程处理任务,如果当前线程数等于核心线程数,则在队列中等待。该队列的大小默认为Integer.MZX_VALUE,可手动设置该队列的大小,即【public LinkedBlockingQueue(int capacity)】
③.ArrayBlockingQueue:可以限定队列的长度,如果当前线程数小于核心线程数,创建核心线程处理任务,如果当前线程数达到了核心线程数,则放入队列中等待,如果队列满了,则创建非核心线程处理任务,如果当前总线程数等于maximumPoolSize,对列也满了,则发生错误。
④.DelayQueue:队列内元素必须实现Delayed接口,该队列接收到任务,首先先入队列,只有达到了指定的延时时间,才会执行任务。
6).ThreadFactory threadFactory【创建线程的方式,一般用不上】
7).RejectedExecutionHandler handler【异常抛出,一般用不上】
四、提交任务
通过ThreadPoolExecutor.execute(Runnable command)方法向线程池添加一个任务五、ThreadPoolExecutor处理任务策略总结
1、当前线程数小于核心线程数的情况下,创建核心线程数来处理任务2、当前线程数等于核心线程数时,放入等待队列
3、等待队列满时,创建非核心线程处理任务
4、队列满时,总线程数=maximumPoolSize时,由RejectedExecutionHandler抛出异常
六、常见四种线程池
Java通过Executors提供了四种线程池,这四种线程池都是直接或间接配置ThreadPoolExecutor的参数实现的(一)CachedThreadPool()【可缓存线程池】
1、创建方式
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
2、源码
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
3、总结
1)线程数无限制
2)有空闲线程则复用空闲线程,若无空闲线程则新建线程
(二)newFixedThreadPool()【定长线程池】
1、创建方式
ExecutorService fixed = Executors.newFixedThreadPool(int nThreads);
2、源码
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
3、总结
1)、可控制线程最大并发数(同时执行的线程数)
2)、超出的线程会在队列中等待
(三)newScheduledThreadPool()【支持定时及周期性任务执行】
1、创建方式
ExecutorService scheduledPool = Executors.newScheduledThreadPool(int corePoolSize);
2、源码
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
new DelayedWorkQueue());
}
3、使用方式
3.1、延时执行功能【延迟3秒执行Runnable任务】
scheduledPool.schedule(Runnable,3,TimeUnit.SENCONDS);
3.2、 循环执行功能【循环执行任务,首先延迟3秒执行,1小时循环一次】
scheduledPool.schedule(Runnable,3,1,TimeUnit.HOURS);
(四)newSingleThreadExecutor()【单线程化的线程池】
1、创建方式
ExecutorService singlePool = Executors.newSingleThreadExecutor();
2、源码
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
3、总结
1)、有且仅有一个工作线程执行任务
2)、所有任务按照指定顺序执行,即遵循队列的入队出队规则
*参考:https://www.jianshu.com/p/210eab345423