美文网首页
CachedThreadPool

CachedThreadPool

作者: 舒尔诚 | 来源:发表于2020-06-17 11:33 被阅读0次

    /**

    • CachedThreadPool

    • 可缓存的线程

    • 当有个请求进入线程池内, 线程池将会启用一个线程

    • 当再次有个请求进入线程池内, 并且上个线程未结束, 仍然会启用一个线程

    • 当有线程执行完毕后,这个线程不会被清除, 而是被缓存,当有请求进入时, 直接使用缓存线程调用

    • 跟 fixedThreadPool 类似, 只不过没有上限(最多Integer最大值), 是一种弹性操作

    • 当线程一直不被使用, 缓存最多持续1分钟(AliveTime默认值),就会被线程池销毁

    • 内部是 DelayedWorkQueue
      */
      public class T08_CachedThreadPool {

      public static void main(String[] args) {
      ExecutorService service = Executors.newCachedThreadPool();
      // pool size 为0
      System.out.println(service); // java.util.concurrent.ThreadPoolExecutor@7f31245a[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]cu'

       for (int i = 0; i < 2; i++) {
           service.execute(() -> {
               try {
                   TimeUnit.MILLISECONDS.sleep(500);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread().getName());
           });
       }
       // pool size 变为2 
       System.out.println(service); // java.util.concurrent.ThreadPoolExecutor@7f31245a[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
      
      
       try {
           TimeUnit.SECONDS.sleep(80); // 最多持续1分钟,这里sleep80s
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
      
       // pool size 变为0
       System.out.println(service); // java.util.concurrent.ThreadPoolExecutor@7f31245a[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]
      

      }
      }

    相关文章

      网友评论

          本文标题:CachedThreadPool

          本文链接:https://www.haomeiwen.com/subject/tzgjxktx.html