Executors为快速创建线程池的工具类,其可以创建基于ThreadPoolExecutor、ScheduledThreadPoolExecutor、ForkJoinPool及包装的不可配置的线程池。
1、newFixedThreadPool()创建固定数量的普通线程池
有如下实现:
//nThread为线程池中线程的数量
public static ExecutorService newFixedThreadPool(int nThreads) {
//通过ThreadPoolExecutor实现类,创建核心线程数及最大线程数都为nThreads的线程池
//空闲超时时间为0,且由于核心线程数和最大线程数一样,核心线程不会空闲超时后被回收
//LinkedBlockingQueue为任务队列实现
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
//nThread为线程池中线程的数量,线程创建的工厂类实现类为threadFactory
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
2、newWorkStealingPool()创建可任务窃取的线程池
有如下实现:
//parallelism并行任务数
public static ExecutorService newWorkStealingPool(int parallelism) {
//基于ForkJoinPool创建线程池;
//使用默认线程创建工厂类
//执行任务异常处理类为null
//任务队列模型为FIFO,true为FIFI,false为FILO
return new ForkJoinPool
(parallelism,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
public static ExecutorService newWorkStealingPool() {
//并行数为处理器的并行数
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
3、newSingleThreadExecutor()创建单个普通线程池
有如下实现:
//创建单个线程的线程池
public static ExecutorService newSingleThreadExecutor() {
//用FinalizableDelegatedExecutorService进行封装,其会在
//finalize()方法中调用线程池的shutdown()方法,对线程池进行关闭
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
4、newCachedThreadPool()创建线程数量不限的普通线程池
有如下实现:
//创建缓存的线程池,即线程池数量不限
public static ExecutorService newCachedThreadPool() {
//核心线程数量为0,最大线程数量为Integer.MAX_VALUE
//阻塞队列为SynchronousQueue
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
5、newSingleThreadScheduledExecutor()创建单个基于时间调度的线程池
有如下实现:
//创建单个可进行时间调度的线程池
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
//利用DelegatedScheduledExecutorService对线程池进行包装
//DelegatedScheduledExecutorService利用门面模式包装了ScheduledExecutorService
//使得用户无法修改ScheduledExecutorService中的一些参数
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
6、newScheduledThreadPool()创建多个基于时间调度的线程池
有如下实现:
//创建固定线程池的可基于时间调度的线程池,corePoolSize为核心线程池测数量
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
7、包装配置不可更改的线程池
有如下实现:
//对ExecutorService类型线程池进行包装,使用户无法对一些参数进行修改
public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
if (executor == null)
throw new NullPointerException();
return new DelegatedExecutorService(executor);
}
//对ScheduledExecutorService类型线程池进行包装,使用户无法对一些参数进行修改
public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {
if (executor == null)
throw new NullPointerException();
return new DelegatedScheduledExecutorService(executor);
}