zoukankan      html  css  js  c++  java
  • newCachedThreadPool的使用

    newCachedThreadPool 线程池特点:

    • 它是一个可以无限扩大的线程池;它比较适合处理执行时间比较小的任务;corePoolSize为0,maximunmPoolSize为无限大,意味着线程数量可以无限大;keepAliveTime为60s,意味着线程空闲时间超过60s就会被杀死;采用SynchronousQueue装等待的任务,这个阻塞队列没有存储空间,这意味着只要有请求到来,就必须要找到一条工作线程处理他,如果当前没有空闲的线程,那么就会再创建一条新的线程
    • 如果线程池长度超过处理需要,可以灵活回收空闲线程;若无可回收,则创建线程

    可以看出该线程池会复用空闲的线程,从而减少创建对象和回收对象带来的开销

    实例一:复用性

    public static void main(String[] args) {
            ExecutorService service = Executors.newCachedThreadPool();
    
            System.out.println(service);
    
            for (int i = 0; i < 20; i++) {
                service.execute(() -> System.out.println(Thread.currentThread().getName()));
            }
    
            System.out.println(service);
            service.shutdown();
        }
    

    输出结果:

    java.util.concurrent.ThreadPoolExecutor@17f052a3[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-2
    pool-1-thread-5
    pool-1-thread-3
    pool-1-thread-4
    pool-1-thread-2
    pool-1-thread-5
    pool-1-thread-5
    pool-1-thread-6
    pool-1-thread-3
    pool-1-thread-5
    pool-1-thread-4
    java.util.concurrent.ThreadPoolExecutor@17f052a3[Running, pool size = 10, active threads = 9, queued tasks = 0, completed tasks = 11]
    pool-1-thread-6
    pool-1-thread-7
    pool-1-thread-2
    pool-1-thread-1
    pool-1-thread-8
    pool-1-thread-10
    pool-1-thread-9
    

    通过输出可以看到有些线程执行完任务后,会空闲下来;有新任务提交时,会利用空闲线程执行。

    实例二:60秒销毁

    public static void main(String[] args) throws InterruptedException {
            ExecutorService service = Executors.newCachedThreadPool();
    
            System.out.println(service);
    
            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());
                });
            }
            System.out.println(service);
            TimeUnit.SECONDS.sleep(70);
            System.out.println(service);
            service.shutdown();
        }
    

    输出结果:

    java.util.concurrent.ThreadPoolExecutor@17f052a3[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
    java.util.concurrent.ThreadPoolExecutor@17f052a3[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
    pool-1-thread-1
    pool-1-thread-2
    java.util.concurrent.ThreadPoolExecutor@17f052a3[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]
    

    在控制台我们看到,输出的service对象里有一个属性pool size,它指的是线程池里的线程数,当过了60秒仍然没有任务来使用线程时,线程会自动释放。

    之所以要用到newCachedThreadPool的原因是:

    • 如果线程池超过长度,可以灵活回收空闲线程,若无可回收,则新建线程。考虑到复用性的问题,如果线程闲置时,可以复用,能够减少损耗系统资源。
    • 它比较适合处理执行时间比较小的任务;我们的任务到了一定上限就会及时处理。(数量1000,等待时间 5000毫秒,也就是说,不管数据过来多少,只要到了1000条或者5秒就发送)
  • 相关阅读:
    [转][c#]C# 二维数组到底该如何定义?
    [c++]筛法求素数
    USB驱动问题
    使用Ajax.dll前台调用后台方法及错误示例
    asp.net中前台javascript与后台C#交互
    visual stdio2010 生成的缓存文件
    jQuery.ajax概述[转]
    一种正向最小匹配的中文分词算法
    2010 .NET面试题整理之基础篇[转]
    Winform设计不规则窗体
  • 原文地址:https://www.cnblogs.com/haohj/p/12801023.html
Copyright © 2011-2022 走看看