Executors
这是一个工具类,主要来了解一下它都提供了哪些方法。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
这两个方法用来创建一个固定线程数量的线程池,采用无限队列。
public static ExecutorService newSingleThreadExecutor() {
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));
}
这两个方法用来创建一个单一线程的线程池,采用无限队列。FinalizableDelegatedExecutorService是Executors的内部类,屏蔽了一些setter方法,防止线程数量等被篡改,所以该方法与newFixedThreadPool(1)不等价。
public static ExecutorService newCachedThreadPool() {
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);
}
这两个方法创建的线程池无核心线程,非核心线程数量不限,空闲时最大存活时间为60s,采用直接传递的队列策略,即每个新来的任务都会有线程处理,不会在队列中等待。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
这两个方法创建ScheduledThreadPoolExecutor。
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
这两个方法创建单一线程的ScheduledThreadPoolExecutor。DelegatedScheduledExecutorService屏蔽了setter方法,防止篡改线程数等配置。
public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
if (executor == null)
throw new NullPointerException();
return new DelegatedExecutorService(executor);
}
public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {
if (executor == null)
throw new NullPointerException();
return new DelegatedScheduledExecutorService(executor);
}
这两个方法对已有的Executor进行包装,防止配置被篡改。
public static ThreadFactory defaultThreadFactory() {
return new DefaultThreadFactory();
}
获取默认的ThreadFactory。
public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}
public static Callable<Object> callable(Runnable task) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<Object>(task, null);
}
将Runnable转换成Callable对象。result为Callable的返回值。
自API24起,提供了两个新的线程池方法:
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);
}
这两个方法会创建一个有固定并行级别的线程池,默认是处理器的数量。并行级别决定了同一时刻最多有几个线程同时执行。
注意到这里有一个新的类ForkJoinPool,该类与ThreadPoolService一样继承自AbstractExecutorService,它实现了工作窃取算法,可以将大任务分解小任务来执行。
至此,我们总共接触到了7种类型的线程池:
- newFixedThreadPool——固定线程数量的线程池,无限队列。
- newSingleThreadExecutor——单线程的线程池,无限队列。
- newCachedThreadPool——任意数量线程的线程池,直接传递。
- newScheduledThreadPool——多线程执行周期性任务的线程池,无限队列。
- newSingleThreadScheduledExecutor——单线程执行周期性任务的线程池,无限队列。
- newWorkStealingPool——指定并行级别的线程池,无限队列。
- ForkJoinPool——将大任务分解为小任务执行的线程池,无限队列。
网友评论