Executor

public interface Executor {
void execute(Runnable command);
}

ExecutorService

ExecutorService是一个接口,继承了Executor接口,定义了一些生命周期的方法

public interface ExecutorService extends Executor {

    //顺次地关闭ExecutorService,停止接收新的任务,等待所有已经提交的任务执行完毕之后,关闭ExecutorService
void shutdown(); //阻止等待任务启动并试图停止当前正在执行的任务,停止接收新的任务,返回处于等待的任务列表
List<Runnable> shutdownNow(); //判断线程池是否已经关闭
boolean isShutdown(); //如果关闭后所有任务都已完成,则返回 true。注意,除非首先调用 shutdown 或 shutdownNow,否则 isTerminated 永不为 true。
boolean isTerminated(); //等待(阻塞)直到关闭或最长等待时间或发生中断,如果此执行程序终止,则返回 true;如果终止前超时期满,则返回 false
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException; //提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。该 Future 的 get 方法在成功完成时将会返回该任务的结果。
<T> Future<T> submit(Callable<T> task); //提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。该 Future 的 get 方法在成功完成时将会返回给定的结果。
<T> Future<T> submit(Runnable task, T result); //提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。该 Future 的 get 方法在成功完成时将会返回 null
Future<?> submit(Runnable task); <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException; <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException; <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException; <T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}

Executors工厂类

Executors类,提供了一系列工厂方法用于创建线程池,返回的线程池都实现了ExecutorService接口。

public class Executors {

    public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
} 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);
} 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));
} 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);
} public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
} public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
} public static ThreadFactory defaultThreadFactory() {
return new DefaultThreadFactory();
} public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}
}

线程池分类

newCachedThreadPool():
缓存型池子:
先查看池中有没有以前建立的线程,如果有,就reuse。如果没有,就建一个新的线程加入池中。 缓存型池子通常用于执行一些生存期很短的异步型任务。
缺省timeout是60s,超过这个IDLE时长,线程实例将被终止及移出池。 cache池线程数支持0-Integer.MAX_VALUE(显然完全没考虑主机的资源承受能力),60秒IDLE newFixedThreadPool(int nThreads):
任意时间点,最多只能有固定数目的活动线程存在。
此时如果有新的线程要建立,只能放在另外的队列中等待,直到当前的线程中某个线程终止直接被移出池子。 FixedThreadPool多数针对一些很稳定很固定的正规并发线程,多用于服务器 newScheduledThreadPool(int corePoolSize):
调度型线程池
这个池子里的线程可以按schedule依次delay执行,或周期执行 SingleThreadExecutor():
单例线程,任意时间池中只能有一个线程
用的是和cache池和fixed池相同的底层池,但线程数目是1,0秒IDLE(无IDLE)

最新文章

  1. Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).
  2. BZOJ2599——[IOI2011]Race
  3. vs2008编译openssl问题
  4. javascript中的this应用
  5. php处理数组函数大全
  6. 分享一下一款直播App开发的过程
  7. webpack需要全局安装,才能使用webpack命令
  8. android反编译工具总结
  9. 开发软件设计模型 visual studio UML
  10. LightOJ 1341 Aladdin and the Flying Carpet 数学
  11. mkpasswd
  12. CAS+SSO原理浅谈
  13. validate插件深入篇
  14. How to find configuration file MySQL uses?(转)
  15. 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)介绍(二)
  16. Python中tuple的功能介绍
  17. Installation Oracle11gR2 RAC---常见报错处理
  18. 03.windows系统重新分配ip的cmd命令
  19. Ubuntu读取/root/.profile时发现错误:mesg:ttyname fa
  20. Linux系统下/tmp目录文件重启后自动删除,不重启自动删除10天前的/TMP的文件(转)

热门文章

  1. Nginx基础优化
  2. VMware下Ubuntu全屏显示
  3. Github Statistics 一个基于 React 的 GitHub 数据统计工具
  4. koa2 的处理请求体koa-bodyparser koa-router 的中间件的学习
  5. centos 6.5 修改主机名
  6. Case Closed?
  7. react-router v3和v4区别
  8. Ubuntu下终端命令安装sublime
  9. Service6
  10. webpack 添加eslint代码审查