一、前提

  1. /**
  2. * 线程运行demo,运行时打出线程id以及传入线程中参数
  3. */
  4. public class ThreadRunner implements Runnable {
  5.  
  6. private final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");
  7.  
  8. /**
  9. * 线程私有属性,创建线程时创建
  10. */
  11. private Integer num;
  12.  
  13. public ThreadRunner(Integer num) {
  14. this.num = num;
  15. }
  16.  
  17. @Override
  18. public void run() {
  19. System.out.println("thread:" + Thread.currentThread().getName() + ",time:" + format.format(new Date()) + ",num:" + num);
  20. try {//使线程睡眠,模拟线程阻塞情况
  21. TimeUnit.SECONDS.sleep(1);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }

二、分类

1、FixedThreadPool-有一个固定大小的线程池

  1. public class FixedThreadPoolDemo {
  2.  
  3. public static void main(String[] args) {
  4. ExecutorService pool = Executors.newFixedThreadPool(4);
  5. for(int i = 0 ; i < 50 ; i++){
  6. pool.submit(new ThreadRunner((i + 1)));
  7. }
  8. pool.shutdown();
  9. }
  10. }
  11. thread:pool-1-thread-2,time:16:14:45.677,num:2
  12. thread:pool-1-thread-4,time:16:14:45.678,num:4
  13. thread:pool-1-thread-3,time:16:14:45.680,num:3
  14. thread:pool-1-thread-1,time:16:14:45.684,num:1
  15. thread:pool-1-thread-4,time:16:14:46.680,num:5
  16. thread:pool-1-thread-2,time:16:14:46.680,num:6
  17. thread:pool-1-thread-3,time:16:14:46.680,num:7
  18. thread:pool-1-thread-1,time:16:14:46.684,num:8
  19. thread:pool-1-thread-4,time:16:14:47.680,num:9
  20. thread:pool-1-thread-2,time:16:14:47.680,num:10
  21. thread:pool-1-thread-3,time:16:14:47.681,num:11
  22. thread:pool-1-thread-1,time:16:14:47.684,num:12
  23. thread:pool-1-thread-4,time:16:14:48.681,num:13
  24. thread:pool-1-thread-2,time:16:14:48.681,num:14
  25. thread:pool-1-thread-3,time:16:14:48.681,num:15
  26. thread:pool-1-thread-1,time:16:14:48.684,num:16
  27. thread:pool-1-thread-4,time:16:14:49.681,num:17
  28. thread:pool-1-thread-2,time:16:14:49.682,num:18
  29. thread:pool-1-thread-3,time:16:14:49.682,num:19
  30. thread:pool-1-thread-1,time:16:14:49.684,num:20
  31. thread:pool-1-thread-4,time:16:14:50.681,num:21
  32. thread:pool-1-thread-2,time:16:14:50.682,num:22
  33. thread:pool-1-thread-3,time:16:14:50.682,num:23
  34. thread:pool-1-thread-1,time:16:14:50.684,num:24
  35. thread:pool-1-thread-4,time:16:14:51.681,num:25
  36. thread:pool-1-thread-2,time:16:14:51.682,num:26
  37. thread:pool-1-thread-3,time:16:14:51.682,num:27
  38. thread:pool-1-thread-1,time:16:14:51.684,num:28
  39. thread:pool-1-thread-4,time:16:14:52.681,num:29
  40. thread:pool-1-thread-2,time:16:14:52.682,num:30
  41. thread:pool-1-thread-3,time:16:14:52.682,num:31
  42. thread:pool-1-thread-1,time:16:14:52.684,num:32
  43. thread:pool-1-thread-4,time:16:14:53.681,num:33
  44. thread:pool-1-thread-2,time:16:14:53.682,num:34
  45. thread:pool-1-thread-3,time:16:14:53.683,num:35
  46. thread:pool-1-thread-1,time:16:14:53.685,num:36
  47. thread:pool-1-thread-2,time:16:14:54.682,num:38
  48. thread:pool-1-thread-4,time:16:14:54.682,num:37
  49. thread:pool-1-thread-3,time:16:14:54.683,num:39
  50. thread:pool-1-thread-1,time:16:14:54.686,num:40
  51. thread:pool-1-thread-2,time:16:14:55.682,num:41
  52. thread:pool-1-thread-4,time:16:14:55.682,num:42
  53. thread:pool-1-thread-3,time:16:14:55.683,num:43
  54. thread:pool-1-thread-1,time:16:14:55.686,num:44
  55. thread:pool-1-thread-2,time:16:14:56.682,num:45
  56. thread:pool-1-thread-4,time:16:14:56.683,num:46
  57. thread:pool-1-thread-3,time:16:14:56.684,num:47
  58. thread:pool-1-thread-1,time:16:14:56.686,num:48
  59. thread:pool-1-thread-2,time:16:14:57.683,num:49
  60. thread:pool-1-thread-4,time:16:14:57.683,num:50

总结: 
- 池中线程数量固定,不会发生变化 
- 使用无界的LinkedBlockingQueue,要综合考虑生成与消费能力,生成过剩,可能导致堆内存溢出。 
- 适用一些很稳定很固定的正规并发线程,多用于服务器

2、CachedThreadPool

  1. public class CachedThreadPoolDemo {
  2.  
  3. public static void main(String[] args) {
  4. ExecutorService pool = Executors.newCachedThreadPool();
  5. for(int i = 0 ; i < 50 ; i++){
  6. pool.submit(new ThreadRunner((i + 1)));
  7. }
  8. pool.shutdown();
  9. }
  10. }
  1. thread:pool-1-thread-2,time:16:17:21.289,num:2
  2. thread:pool-1-thread-3,time:16:17:21.290,num:3
  3. thread:pool-1-thread-4,time:16:17:21.290,num:4
  4. thread:pool-1-thread-6,time:16:17:21.291,num:6
  5. thread:pool-1-thread-7,time:16:17:21.291,num:7
  6. thread:pool-1-thread-8,time:16:17:21.291,num:8
  7. thread:pool-1-thread-1,time:16:17:21.292,num:1
  8. thread:pool-1-thread-10,time:16:17:21.293,num:10
  9. thread:pool-1-thread-5,time:16:17:21.294,num:5
  10. thread:pool-1-thread-11,time:16:17:21.294,num:11
  11. thread:pool-1-thread-15,time:16:17:21.294,num:15
  12. thread:pool-1-thread-9,time:16:17:21.294,num:9
  13. thread:pool-1-thread-16,time:16:17:21.295,num:16
  14. thread:pool-1-thread-20,time:16:17:21.295,num:20
  15. thread:pool-1-thread-14,time:16:17:21.296,num:14
  16. thread:pool-1-thread-12,time:16:17:21.296,num:12
  17. thread:pool-1-thread-19,time:16:17:21.297,num:19
  18. thread:pool-1-thread-13,time:16:17:21.299,num:13
  19. thread:pool-1-thread-17,time:16:17:21.300,num:17
  20. thread:pool-1-thread-18,time:16:17:21.302,num:18
  21. thread:pool-1-thread-22,time:16:17:21.304,num:22
  22. thread:pool-1-thread-23,time:16:17:21.304,num:23
  23. thread:pool-1-thread-24,time:16:17:21.305,num:24
  24. thread:pool-1-thread-21,time:16:17:21.305,num:21
  25. thread:pool-1-thread-26,time:16:17:21.305,num:26
  26. thread:pool-1-thread-25,time:16:17:21.306,num:25
  27. thread:pool-1-thread-29,time:16:17:21.307,num:29
  28. thread:pool-1-thread-28,time:16:17:21.308,num:28
  29. thread:pool-1-thread-30,time:16:17:21.308,num:30
  30. thread:pool-1-thread-34,time:16:17:21.308,num:34
  31. thread:pool-1-thread-35,time:16:17:21.308,num:35
  32. thread:pool-1-thread-33,time:16:17:21.308,num:33
  33. thread:pool-1-thread-27,time:16:17:21.309,num:27
  34. thread:pool-1-thread-32,time:16:17:21.308,num:32
  35. thread:pool-1-thread-31,time:16:17:21.309,num:31
  36. thread:pool-1-thread-36,time:16:17:21.310,num:36
  37. thread:pool-1-thread-37,time:16:17:21.310,num:37
  38. thread:pool-1-thread-38,time:16:17:21.310,num:38
  39. thread:pool-1-thread-42,time:16:17:21.310,num:42
  40. thread:pool-1-thread-40,time:16:17:21.310,num:40
  41. thread:pool-1-thread-41,time:16:17:21.311,num:41
  42. thread:pool-1-thread-47,time:16:17:21.762,num:47
  43. thread:pool-1-thread-43,time:16:17:21.762,num:43
  44. thread:pool-1-thread-39,time:16:17:21.762,num:39
  45. thread:pool-1-thread-45,time:16:17:21.762,num:45
  46. thread:pool-1-thread-44,time:16:17:21.763,num:44
  47. thread:pool-1-thread-46,time:16:17:21.761,num:46
  48. thread:pool-1-thread-48,time:16:17:21.761,num:48
  49. thread:pool-1-thread-49,time:16:17:21.765,num:49
  50. thread:pool-1-thread-50,time:16:17:21.765,num:50

总结 
- 池中线程时随着处理数据增加而增加 
- 线程数并不是一直增加,如果有新任务需要执行时,首先查询池中是否有空闲线程并且还为到空闲截止时间,如果有,则使用空闲线程,如果没有,则创建新线程并放入池中。 
- 用于执行一些生存期很短的异步型任务。不适用于IO等长延时操作,因为这可能会创建大量线程,导致系统崩溃。 
- 使用SynchronousQueue作为阻塞队列,如果有新任务进入队列,必须队列中数据被其他线程处理,否则会等待。

3、SingleThreadExecutor

  1. public class SingleThreadPoolDemo {
  2.  
  3. public static void main(String[] args) {
  4. ExecutorService pool = Executors.newSingleThreadExecutor();
  5. for(int i = 0 ; i < 50 ; i++){
  6. pool.submit(new ThreadRunner((i + 1)));
  7. }
  8. pool.shutdown();
  9. }
  10. }
  1. thread:pool-1-thread-1,time:16:20:10.194,num:1
  2. thread:pool-1-thread-1,time:16:20:11.197,num:2
  3. thread:pool-1-thread-1,time:16:20:12.197,num:3
  4. thread:pool-1-thread-1,time:16:20:13.197,num:4
  5. thread:pool-1-thread-1,time:16:20:14.197,num:5
  6. thread:pool-1-thread-1,time:16:20:15.198,num:6
  7. thread:pool-1-thread-1,time:16:20:16.198,num:7
  8. thread:pool-1-thread-1,time:16:20:17.198,num:8
  9. thread:pool-1-thread-1,time:16:20:18.198,num:9
  10. thread:pool-1-thread-1,time:16:20:19.198,num:10
  11. thread:pool-1-thread-1,time:16:20:20.198,num:11
  12. thread:pool-1-thread-1,time:16:20:21.199,num:12
  13. thread:pool-1-thread-1,time:16:20:22.200,num:13
  14. thread:pool-1-thread-1,time:16:20:23.200,num:14
  15. thread:pool-1-thread-1,time:16:20:24.200,num:15
  16. thread:pool-1-thread-1,time:16:20:25.200,num:16
  17. thread:pool-1-thread-1,time:16:20:26.201,num:17
  18. thread:pool-1-thread-1,time:16:20:27.201,num:18
  19. thread:pool-1-thread-1,time:16:20:28.201,num:19
  20. thread:pool-1-thread-1,time:16:20:29.201,num:20
  21. thread:pool-1-thread-1,time:16:20:30.202,num:21
  22. thread:pool-1-thread-1,time:16:20:31.202,num:22
  23. thread:pool-1-thread-1,time:16:20:32.203,num:23
  24. thread:pool-1-thread-1,time:16:20:33.203,num:24
  25. thread:pool-1-thread-1,time:16:20:34.203,num:25
  26. thread:pool-1-thread-1,time:16:20:35.203,num:26
  27. thread:pool-1-thread-1,time:16:20:36.203,num:27
  28. thread:pool-1-thread-1,time:16:20:37.203,num:28
  29. thread:pool-1-thread-1,time:16:20:38.203,num:29
  30. thread:pool-1-thread-1,time:16:20:39.203,num:30
  31. thread:pool-1-thread-1,time:16:20:40.203,num:31
  32. thread:pool-1-thread-1,time:16:20:41.203,num:32
  33. thread:pool-1-thread-1,time:16:20:42.203,num:33
  34. thread:pool-1-thread-1,time:16:20:43.204,num:34
  35. thread:pool-1-thread-1,time:16:20:44.204,num:35
  36. thread:pool-1-thread-1,time:16:20:45.204,num:36
  37. thread:pool-1-thread-1,time:16:20:46.204,num:37
  38. thread:pool-1-thread-1,time:16:20:47.205,num:38
  39. thread:pool-1-thread-1,time:16:20:48.205,num:39
  40. thread:pool-1-thread-1,time:16:20:49.205,num:40
  41. thread:pool-1-thread-1,time:16:20:50.206,num:41
  42. thread:pool-1-thread-1,time:16:20:51.206,num:42
  43. thread:pool-1-thread-1,time:16:20:52.207,num:43
  44. thread:pool-1-thread-1,time:16:20:53.207,num:44
  45. thread:pool-1-thread-1,time:16:20:54.207,num:45
  46. thread:pool-1-thread-1,time:16:20:55.207,num:46
  47. thread:pool-1-thread-1,time:16:20:56.207,num:47
  48. thread:pool-1-thread-1,time:16:20:57.208,num:48
  49. thread:pool-1-thread-1,time:16:20:58.208,num:49
  50. thread:pool-1-thread-1,time:16:20:59.209,num:50

总结: 
- 线程中只有一个线程在执行 
- 适用于有明确执行顺序但是不影响主线程的任务,压入池中的任务会按照队列顺序执行。 
- 使用无界的LinkedBlockingQueue,要综合考虑生成与消费能力,生成过剩,可能导致堆内存溢出。

三、源码

  1. public static ExecutorService newFixedThreadPool(int nThreads) {
  2. return new ThreadPoolExecutor(nThreads, nThreads,
  3. 0L, TimeUnit.MILLISECONDS,
  4. new LinkedBlockingQueue<Runnable>());
  5. public static ExecutorService newCachedThreadPool() {
  6. return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
  7. 60L, TimeUnit.SECONDS,
  8. new SynchronousQueue<Runnable>());
  9. public static ExecutorService newSingleThreadExecutor() {
  10. return new FinalizableDelegatedExecutorService
  11. (new ThreadPoolExecutor(1, 1,
  12. 0L, TimeUnit.MILLISECONDS,
  13. new LinkedBlockingQueue<Runnable>()));
  14. }

 ThreadPoolExecutor 构造方法*

  1. public ThreadPoolExecutor(int corePoolSize,
  2. int maximumPoolSize,
  3. long keepAliveTime,
  4. TimeUnit unit,
  5. BlockingQueue<Runnable> workQueue) {
  6. this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
  7. Executors.defaultThreadFactory(), defaultHandler);
  8. }
  9.  
  10. public ThreadPoolExecutor(int corePoolSize,
  11. int maximumPoolSize,
  12. long keepAliveTime,
  13. TimeUnit unit,
  14. BlockingQueue<Runnable> workQueue,
  15. ThreadFactory threadFactory,
  16. RejectedExecutionHandler handler) {
  17. if (corePoolSize < 0 ||
  18. maximumPoolSize <= 0 ||
  19. maximumPoolSize < corePoolSize ||
  20. keepAliveTime < 0)
  21. throw new IllegalArgumentException();
  22. if (workQueue == null || threadFactory == null || handler == null)
  23. throw new NullPointerException();
  24. this.corePoolSize = corePoolSize;
  25. this.maximumPoolSize = maximumPoolSize;
  26. this.workQueue = workQueue;
  27. this.keepAliveTime = unit.toNanos(keepAliveTime);
  28. this.threadFactory = threadFactory;
  29. this.handler = handler;
  30. }
    • corePoolSize:线程池核心线程数量 
      1. 如果池中线程数量少于核心线程池数量,则直接新建线程处理当前任务。
      2. 核心线程池空闲不会被回收。
      3. 当池中无空闲线程时,新任务将被添加到阻塞队列
    • maximumPoolSize:线程池最大线程数量 
      1. 当阻塞队列已满,并且有新任务还在入队时,创建新的线程处理,直到线程数大于maximumPoolSize。
      2. 超出corePoolSize部分的线程超过空闲时间后会被回收
      3. 当线程已经超出corePoolSize,并且队列容量已满,则拒绝入队。
    • keepAliveTime unit:线程存活时间 
      1. 当线程超出corePoolSize时生效
      2. 线程空余keepAliveTime后,将被回收
    • workQueue:线程使用阻塞队列
    • threadFactory:创建线程池工厂 
      1. 用于控制创建线程或者销毁线程时加入其它逻辑
    • handler:线程池拒绝策略 
      1. 直接丢弃(DiscardPolicy)
      2. 丢弃队列中最老的任务(DiscardOldestPolicy)。
      3. 抛异常(AbortPolicy)
      4. 将任务分给调用线程来执行(CallerRunsPolicy)

Java线程池(ExecutorService)使用的更多相关文章

  1. [Java线程] Java线程池ExecutorService

    示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...

  2. 【Java线程】Java线程池ExecutorService

    示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...

  3. Java线程池ExecutorService和CountDownLatch的小例子

    import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...

  4. Java线程池 ExecutorService

    一.ExecutorService介绍 ExecutorService是Java中对线程池定义的一个接口,它java.util.concurrent包中,在这个接口中定义了和后台任务执行相关的方法:  ...

  5. Java线程池 ExecutorService了解一下

    本篇主要涉及到的是java.util.concurrent包中的ExecutorService.ExecutorService就是Java中对线程池的实现. 一.ExecutorService介绍 E ...

  6. Java线程池ExecutorService

    开篇前,我们先来看看不使用线程池的情况: new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override ...

  7. Java线程池ExecutorService 代码备忘

    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5)创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待 p ...

  8. java 线程池(ExecutorService与Spring配置threadPoolTaskExecutor)

    一.java ExecutorService实现 创建ExecutorService变量private ExecutorService executor = null 2.执行对应任务时,首先生成线程 ...

  9. java 线程池--ExecutorService

    一 Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. new ...

  10. PAIP.并发编程 多核编程 线程池 ExecutorService的判断线程结束

    PAIP.并发编程 多核编程 线程池 ExecutorService的判断线程结束 ExecutorService并没有提供什么 isDone()或者isComplete()之类的方法. 作者Atti ...

随机推荐

  1. 【转帖】极简Docker和Kubernetes发展史

    极简Docker和Kubernetes发展史 https://www.cnblogs.com/chenqionghe/p/11454248.html 2013年 Docker项目开源 2013年,以A ...

  2. IIS提速的几个优化

    一.内存池右键高级设置 1.设置队列5000 2.设置固定回收时间 3.设置空闲时间Suspend 二.网站右键高级设置 1.启用预加载

  3. CLRS10.2-7练习 - 翻转单向列表

    要求: Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of nelements. The pro ...

  4. pytest_使用自定义标记mark

    前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是i ...

  5. Selenium+Java(八)Selenium下拉框处理

    Selenium定位下拉框中的元素与普通元素定位有所不同,下面介绍三种定位下拉框元素的方法. 下拉款HTML代码如图所示: 一.通过text定位 //获取下拉框对象 Select city = new ...

  6. AspNetCore MVC页面数据提交验证

    2019/05/14,AspNetCore 2.2.0 摘要:AspNetCore MVC使用数据注释配合jquery.validate提交页面进行前端加后端的数据验证 主要用到了jquery.val ...

  7. 如何在linux中重置Mysql访问密码

    目录 跳过密码认证 重启MySQL: 用sql来修改root的密码 去掉'跳过密码'代码 假设我们使用的是root账户. 跳过密码认证 重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: # ...

  8. 2019 龙采科技java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.龙采科技等公司offer,岗位是Java后端开发,最终选择去了龙采科技. 面试了很多家公司,感觉大部分公司考察的点 ...

  9. selenium自动化测试框架之PO设计模式

    面向对象的特性:封装.继承.多态.在自动化中一样适用,Selenium自动化测试中有一个名字常常被提及PageObject(思想与面向对象的特性相同),通过PO模式可以大大提高测试用例的维护效率. 传 ...

  10. 英伟达 cuda 开发套件下载

    下载地址 https://developer.nvidia.com/cuda-toolkit 安装比较简单,就不多说了.