同步线程池类图
先来一个同步线程池的类关系图 总结在下面,有不妥之处请指正,谢谢!
类关系图总结
1:ForkJoin和ThreadPool还是需要实现Runnable来完成线程池。
2:核心是AbstractExecutorService、RunnableFuture和ForkJoinTask(箭头指向最多 类似扇入)
3:提供开放的调用者ThreadPoolExecutor、ForkJoinPool
4:提供模板式访问工具Executors
其他描述
interface Executor 按给定时间执行任务(实现java.lang.Runnable接口) 。根据实现可以在新线程、线程池或者执行中线程来执行。
interface Runnable
interface RunnableFuture<V> extends Runnable, Future<V>//java.util.concurrent
interface ExecutorService extends Executor
abstract class AbstractExecutorService implements ExecutorService
ThreadPoolExecutor
主要参数
//通过addWorker创建线程,工厂大小取决于操作系统或者用户级的限制策略,创建时可能会抛出 OutOfMemoryError异常
private volatile ThreadFactory threadFactory;
//线程池满或者shutdown 时的处理器
private volatile RejectedExecutionHandler handler;
//空闲等待超时时间 单位是纳秒 设置corePoolSize或者allowCoreThreadTimeOut=true时生效
private volatile long keepAliveTime;
//默认false,表示核心业务线程空闲时也保持活动状态,设置为true时,keepAliveTime的值为等待超时时间
private volatile boolean allowCoreThreadTimeOut;
//活跃线程池大小(不允许超时),设置 allowCoreThreadTimeOut 时 活跃线程池最小值为0
private volatile int corePoolSize;
//线程池的最大大小 实际取决于 CAPACITY
private volatile int maximumPoolSize;
// 默认拒绝处理的策略处理器
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
创建姿势
创建参数
corePoolSize
keepAliveTime
maximumPoolSize (maximumPoolSize < corePoolSize)
workQueue 非空 ,否则 NPE
threadFactory 非空 ,否则 NPE
handler 非空, 否则 NPE
4种,但其实最终仍是调用一种完成
姿势一
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
//实际是自调用如下
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler)
姿势二
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory)
//实际是自调用如下
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler)
姿势三
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
//实际是自调用如下
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler)
姿势四
//自调用就是这里!
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
总结
就1点 记住最后的自调用就可以了,另外需要注意 BlockingQueue。
拓展一下,RejectedExecutionHandler 也可以自定义
Executors的五种线程池介绍
分类
类型 | 含义 |
---|---|
FixedThreadPool | 固定大小线程池 |
CachedThreadPool | 动态线程池 |
SingleThreadScheduledExecutor | 单例线程 |
ScheduledThreadPool | 任务计划线程池 |
WorkStealingPool | 抢占工作线程池 |
FixedThreadPool
仅指定大小
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
指定大小和factory
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
CachedThreadPool
无参创建 最大线程池大小为 Integer.MAX_VALUE
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
指定factory 最大线程池大小为 Integer.MAX_VALUE
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
SingleThreadScheduledExecutor
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
ScheduledThreadPool
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
WorkStealingPool
public static ExecutorService newWorkStealingPool(int parallelism) {
return new ForkJoinPool
(parallelism,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
5种姿势总结
1:创建的线程池基本分为有factory和无factory两种
2:CachedPool最大Integer.MAX_VALUE 可能会造成内存溢出 ,或者运行时异常,
并且线程池数量变化不可控
3:抢占工作线程池可设定优先级,但这种具有优先级的线程执行起来也是无序