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<Runnable>());
}

这种方法和上面的方法基本一致。仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。

 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}

这个newSingleThreadExecutor是上面的方法基本一致,仅仅是创建了单线程的线程池

 public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}

这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
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<Runnable>());
}

这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。

 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}

这种方法创建单线程的ScheduledThreadPoolExecutor。DelegatedScheduleExecutorService是个包装类。将ScheduledThreadPoolExecutor的对外接口缩小

 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);
}

这种方法把Runnable接口适配成Callable接口

 public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}

最新文章

  1. 如何让ConfigurationManager打开任意的配置文件
  2. 用javascript比较快速排序和合并排序的优劣
  3. BZOJ1208 宠物收养所
  4. codeforces 468B 2-sat
  5. IIS 之 查看并发连接数
  6. C# Winform 界面线程的Invoke死锁,以及Application.DoEvent的问题
  7. DirectX11中Shader的封装
  8. ThinkPHP中:用户登录权限验证类
  9. day07_Tomcat服务器与http学习笔记
  10. 找出程序GasMileage中的哪一行与下列叙述相对应:
  11. 【PHP】PHP的安装和配置
  12. CentOS6.5运行yum报错:No module named yum
  13. Vuejs核心思想学习笔记
  14. 写给大忙人的nginx核心配置详解
  15. 用jquery将输入的文字的双向绑定
  16. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变
  17. 对spring 对持久层的支持和数据库连接池的理解
  18. highcharts点击事件系列
  19. 虚函数不应该是inlined(More Effective C++ 笔记)
  20. .net core系列之《.net core中使用集成IDistributedCache接口的Redis和MongoDB实现分布式缓存》

热门文章

  1. oracle 用户解锁和修改用户密码
  2. Firefox OS简单介绍
  3. sqlite学习笔记9:C语言中使用sqlite之插入数据
  4. ES API 备忘
  5. [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)
  6. Elasticsearch之四种查询类型和搜索原理(博主推荐)
  7. C++之易混淆知识点二
  8. 洛谷P1067 多项式输出(模拟)
  9. Pyinstaller 1 使用PyInstaller
  10. POJ 3264 Balanced Lineup【线段树】