当前位置:   article > 正文

newCachedThreadPool,缓存池_executorservice cachedthreadpool = executors.newca

executorservice cachedthreadpool = executors.newcachedthreadpool();
Executors.newCachedThreadPool();

无限制创建线程。

  1. package com.itcast.thread;
  2. import java.time.LocalDateTime;
  3. import java.util.PrimitiveIterator;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. /**
  7. * @Description
  8. * @Author by mocar小师兄
  9. * @Date 2019/11/29 12:22
  10. **/
  11. public class NewCachedThreadPool {
  12. public static void main(String[] args) {
  13. ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
  14. for (int index = 0; index <5 ; index++) {
  15. //final int index=num;
  16. System.out.println("循环:" + index + "开始");
  17. cachedThreadPool.execute(() -> {
  18. LocalDateTime startTime = LocalDateTime.now();
  19. System.out.println(Thread.currentThread().getName() + "......开始执行");
  20. try {
  21. Thread.sleep(500);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. System.out.println(Thread.currentThread().getName() + "......执行完成");
  26. LocalDateTime endTime = LocalDateTime.now();
  27. System.out.println(Thread.currentThread().getName() +",耗时:" + (endTime.getSecond()-startTime.getSecond()));
  28. });
  29. System.out.println("循环:" + index + "结束");
  30. }
  31. cachedThreadPool.shutdown();
  32. System.out.println("thread main 结束");
  33. }
  34. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/246536
推荐阅读
相关标签
  

闽ICP备14008679号