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]
    
        } 
    }
  • 相关阅读:
    free和delete把指针怎么啦?
    动态内存会被自动释放吗?
    杜绝“野指针”
    有了malloc/free为什么还要new/delete ?
    Linux Shell Bash 带有特殊含义的退出码
    centos中更换jdk的版本
    shift移动变量
    shell script针对参数已经有配置好变量名称
    输入两个数后输出相乘的结果
    输入变量
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11719720.html
Copyright © 2011-2022 走看看