zoukankan      html  css  js  c++  java
  • CachedThreadPool

    /**
     * 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]
    
        } 
    }
  • 相关阅读:
    Two strings CodeForces
    Dasha and Photos CodeForces
    Largest Beautiful Number CodeForces
    Timetable CodeForces
    Financiers Game CodeForces
    AC日记——整理药名 openjudge 1.7 15
    AC日记——大小写字母互换 openjudge 1.7 14
    AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13
    AC日记——加密的病历单 openjudge 1.7 12
    AC日记——潜伏着 openjudge 1.7 11
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11719720.html
Copyright © 2011-2022 走看看