ThreadPoolExecutor实现类、ScheduledThreadPoolExecutor实现类、Future接口、Runnable接口、Callable接口和Executors。
java.util.concurrent.Executor : 负责线程的使用与调度的根接口
|–ExecutorService:Executor的子接口,线程池的主要接口
|–ThreadPoolExecutor:ExecutorService的实现类
|–ScheduledExecutorService:ExecutorService的子接口,负责线程的调度
|–ScheduledThreadPoolExecutor:既继承了ThreadPoolExecutor,同时实现了ScheduledExecutorService
ThreadPoolExecutor实现类了解:
构造方法:
| public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); } |
参数介绍:
- int corePoolSize, 核心线程大小
- int maximumPoolSize,最大线程大小
- long keepAliveTime, 超过corePoolSize的线程多久不活动被销毁时间
- TimeUnit unit,时间单位
- BlockingQueue
workQueue 任务队列 - ThreadFactory threadFactory 线程池工厂
- RejectedExecutionHandler handler 拒绝策略
以下介绍三种类型的ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool和CachedThreadPool。
这3种线程池都是创建了ThreadPollExecutor对象,只是传递的参数不一样,传入的workQueue 都是默认,即最大可添加Integer.MAX_VALUE个任务,这也就是阿里巴巴java开发规范禁止直接使用java提供的默认线程池的原因。
参考:https://blog.csdn.net/weixin_41888813/article/details/90769126
1、FixedThreadPool
线程数量固定的线程池(定长线程池),可控制线程最大并发数,超出的线程会在队列中等待。
它适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。
线程数量固定的线程池(定长线程池),可控制线程最大并发数,超出的线程会在队列中等待。
它适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。
| public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue } public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue threadFactory); } |
- 初始线程数为:nThreads
- 最大线程数为:nThreads
- 超时时间为:0毫秒
- 阻塞队列采用的是:linkedBlockingQueue
线程池最大线程数量maxmumPoolSize和核心线程池的数量corePoolSize设置为相等,使用LinkedBlockingQueue作为阻塞队列。
2、SingleThreadExecutor
只有一个线程的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
它适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。
只有一个线程的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
它适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。
| public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue } public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue threadFactory)); } |
- 初始线程数为:1
- 最大线程数为:1
- 超时时间为:0毫秒
- 阻塞队列采用的是:linkedBlockingQueue
3、newCachedThreadPool
一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
它适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。
一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
它适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。
| public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue } public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue threadFactory); } |
- 初始线程数为:0
- 最大线程数为: Integer.MAX_VALUE(int的最大值)
- 超时时间为:1分钟
- 阻塞队列采用的是:SynchronousQueue
参考:https://www.cnblogs.com/xiondun/p/14764096.html
参考:https://blog.csdn.net/tongdanping/article/details/79625109
参考:https://blog.csdn.net/qq_43040688/category_9910156.html
参考:https://blog.csdn.net/qq_43040688/article/details/106046629
参考:关于阿里规范禁止使用Executors创建线程池的分析



