Executor框架为了更方便使用,提供了Executors这个工厂类,通过一系列的静态工厂方法,可以快速地创建相应的Executor实例。
只有一个nThreads参数的newFixedThreadPool方法会创建一个ThreadPoolExecutor,corePoolSize和maximumPoolSize都是nThreads,并且keepAliveTime为0表示不会设置过期时间,采用LinkedBlockingQueue作为工作队列
这个方法创建的ThreadPoolExecutor采用固定线程数nThreads,当线程少于nThreads时会为新的任务创建新的Worker工作线程,直到线程数达到nThreads。线程达到nThreads后不会回收。后续新建的任务会进入工作队列,工作队列是无界的。当任务量过大时,可能会因为无界的工作队列造成OOM的问题。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
这个方法和上面的方法基本一致,只是多了一个ThreadFactory,可以自定义创建的线程属性。
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(),
threadFactory);
}
这个newSingleThreadExecutor是上面的方法基本一致,只是创建了单线程的线程池
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue()));
}
这个方法和上面的方法基本一致,只是多了一个ThreadFactory,可以自定义创建的线程属性。
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(),
threadFactory));
}
这个newCachedThreadPool返回一个ThreadPoolExecutor,corePoolSize为0,maximumPoolSize为Integer.MAX_VALUE,表示的意思是线程数没有限制。KeepAliveTime为60秒,表示的意思是当线程空闲时间超过60秒才会回收线程。这个就是所谓的Cache。空闲的意思之前说了,表示Worker在工作队列中取任务时,如果超过60秒没取到任务,这个线程就超时,要被回收。采用了SynchronousQueue同步队列作为工作队列,意思是来一个新任务就把任务交给Worker工作线程,不入队列。如果没有可用的工作线程,就创建新的工作线程。这个方法的问题是当任务量大时,会消耗太多的CPU资源,创建太多线程,增大线程上线文切换等消耗。
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);
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
这个方法和上面的方法基本一致,只是多了一个ThreadFactory,可以自定义创建的线程属性。
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
这个方法创建corePoolSize个线程的ScheduledThreadPoolExecutor。其他特性和newFixedThreadPool(nThreads)一致
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
这个方法和上面的方法基本一致,只是多了一个ThreadFactory,可以自定义创建的线程属性。
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
public static Callable callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter(task, result);
}