FixedThreadPool和CachedThreadPool 的区别_cachedthreadpool和fixed_passion&dj的博客-程序员宅基地

技术标签: 多线程  

正文

CachedThreadPool

CachedThreadPool 是通过 java.util.concurrent.Executors 创建的 ThreadPoolExecutor 实例。这个实例会根据需要,在线程可用时,重用之前构造好的池中线程。这个线程池在执行 大量短生命周期的异步任务时(many short-lived asynchronous task),可以显著提高程序性能。调用 execute时,可以重用之前已构造的可用线程,如果不存在可用线程,那么会重新创建一个新的线程并将其加入到线程池中。如果线程超过 60 秒还未被使用,就会被中止并从缓存中移除。因此,线程池在长时间空闲后不会消耗任何资源。

注意队列实例是:new SynchronousQueue


    /**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to <tt>execute</tt> will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

FixedThreadPool

FixedThreadPool 是通过 java.util.concurrent.Executors 创建的 ThreadPoolExecutor 实例。这个实例会复用 固定数量的线程 处理一个 共享的无边界队列 。任何时间点,最多有 nThreads 个线程会处于活动状态执行任务。如果当所有线程都是活动时,有多的任务被提交过来,那么它会一致在队列中等待直到有线程可用。如果任何线程在执行过程中因为错误而中止,新的线程会替代它的位置来执行后续的任务。所有线程都会一致存于线程池中,直到显式的执行 ExecutorService.shutdown() 关闭。

注意队列实例是:new LinkedBlockingQueue


    /**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * <tt>nThreads</tt> threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

SingleThreadPool

SingleThreadPool 是通过 java.util.concurrent.Executors 创建的 ThreadPoolExecutor 实例。这个实例只会使用单个工作线程来执行一个无边界的队列。(注意,如果单个线程在执行过程中因为某些错误中止,新的线程会替代它执行后续线程)。它可以保证认为是按顺序执行的,任何时候都不会有多于一个的任务处于活动状态。和 newFixedThreadPool(1) 的区别在于,如果线程遇到错误中止,它是无法使用替代线程的。

    
    /**
     * Creates an Executor that uses a single worker thread operating
     * off an unbounded queue. (Note however that if this single
     * thread terminates due to a failure during execution prior to
     * shutdown, a new one will take its place if needed to execute
     * subsequent tasks.)  Tasks are guaranteed to execute
     * sequentially, and no more than one task will be active at any
     * given time. Unlike the otherwise equivalent
     * <tt>newFixedThreadPool(1)</tt> the returned executor is
     * guaranteed not to be reconfigurable to use additional threads.
     *
     * @return the newly created single-threaded Executor
     */
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

程序演示

  • LiftOff

    
        public class LiftOff implements Runnable {
            protected int countDown = 10; // Default
            private static int taskCount = 0;
            private final int id = taskCount++;
    
            public LiftOff() {}
    
            public LiftOff(int countDown) {
                this.countDown = countDown;
            }
    
            public String status() {
                return "Thread ID: [" + String.format("%3d", Thread.currentThread().getId()) + "] #" + id + "(" + (countDown > 0 ? countDown : "LiftOff!") + ") ";
            }
    
            @Override
            public void run() {
                while (countDown-- > 0) {
                    System.out.println(status());
                    Thread.yield();
                }
            }
    
        }   
    
  • CachedThreadPoolCase

    
        public class CachedThreadPoolCase {
            public static void main(String[] args) throws InterruptedException {
                ExecutorService exec = Executors.newCachedThreadPool();
                for(int i = 0; i < 5; i++) {
                    exec.execute(new LiftOff());
                    Thread.sleep(10);
                }
                exec.shutdown();
            }
        }   
    

    当 sleep 间隔为 5 milliseconds 时,共创建了 3 个线程,并交替执行。

    当 sleep 间隔为 10 milliseconds 时,共创建了 2 个线程,交替执行。

  • FixedThreadPoolCase

    
        public class FixedThreadPoolCase {
    
            public static void main(String[] args) throws InterruptedException {
                ExecutorService exec = Executors.newFixedThreadPool(3);
                for (int i = 0; i < 5; i++) {
                    exec.execute(new LiftOff());
                    Thread.sleep(10);
                }
                exec.shutdown();
            }
        }   
    

    无论 sleep 间隔时间是多少,总共都创建 3 个线程,并交替执行。

  • SingleThreadCase

    
        public class SingleThreadPoolCase {
    
            public static void main(String[] args) throws InterruptedException {
                ExecutorService exec = Executors.newSingleThreadExecutor();
                for (int i = 0; i < 10; i++) {
                    exec.execute(new LiftOff());
                    Thread.sleep(5);
                }
                exec.shutdown();
            }
        }
    

    无论 sleep 间隔时间是多少,总共都只创建 1 个线程。

FixedThreadPool 与 CachedThreadPool 特性对比

特性 FixedThreadPool CachedThreadPool
重用 FixedThreadPool 与 CacheThreadPool差不多,也是能 reuse 就用,但不能随时建新的线程 缓存型池子,先查看池中有没有以前建立的线程,如果有,就 reuse ;如果没有,就建一个新的线程加入池中
池大小 可指定 nThreads,固定数量 可增长,最大值 Integer.MAX_VALUE
队列大小 无限制 无限制
超时 无 IDLE 默认 60 秒 IDLE
使用场景 所以 FixedThreadPool 多数针对一些很稳定很固定的正规并发线程,多用于服务器 大量短生命周期的异步任务
结束 不会自动销毁 注意,放入 CachedThreadPool 的线程不必担心其结束,超过 TIMEOUT 不活动,其会自动被终止。

最佳实践

FixedThreadPool 和 CachedThreadPool 两者对高负载的应用都不是特别友好。

CachedThreadPool 要比 FixedThreadPool 危险很多。

如果应用要求高负载、低延迟,最好不要选择以上两种线程池:

  1. 任务队列的无边界:会导致内存溢出以及高延迟
  2. 长时间运行会导致 CachedThreadPool 在线程创建上失控

因为两者都不是特别友好,所以推荐使用 ThreadPoolExecutor ,它提供了很多参数可以进行细粒度的控制。

  1. 将任务队列设置成有边界的队列
  2. 使用合适的 RejectionHandler - 自定义的 RejectionHandler 或 JDK 提供的默认 handler 。
  3. 如果在任务完成前后需要执行某些操作,可以重载

     beforeExecute(Thread, Runnable)
     afterExecute(Runnable, Throwable)
  4. 重载 ThreadFactory ,如果有线程定制化的需求
  5. 在运行时动态控制线程池的大小(Dynamic Thread Pool

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/denghonghao/article/details/82428531

智能推荐

Linux上查找最大文件的 3 种方法-程序员宅基地

作者:CloudDeveloper来源:公众号「Linux云计算网络」有时候我们在系统上安装了数十个应用程序,随着使用时间的推移,许多文件变得越来越大,从而导致磁盘空间...

master使用-程序员宅基地

可以在内容页中编写代码来引用母版页中的属性、方法和控件,但这种引用有一定的限制。对于属性和方法的规则是:如果它们在母版页上被声明为公共成员,则可以引用它们。这包括公共属性和公共方法。在引用母版页上的控件时,没有只能引用公共成员的这种限制。引用母版页上的公共成员1.在内容页中添加 @ MasterType 指令。在该指令中,将 VirtualPath 属性设置为母版页的位置,如下面的示例所示:

树的孩子链表存储结构-程序员宅基地

树,孩子链表_树的孩子链表存储结构

win7/win8 64位系统注册TeeChart8.ocx 控件---以及dllregisterserver调用失败问题解决办法-程序员宅基地

TeeChart控件就不多介绍了,很多朋友不知道开始怎么注册使用,尤其是在64位系统下如何注册的问题,具体如下:win7、win8 64位系统问题所在:64位的系统一般都是可以安装32位程序的执行C:\Windows\SysWOW64\regsvr32.exe,而不是C:\Windows\System32\regsvr32.exe 权限不够出现dllregisterserver

Gets和scanf的区别_scanf和gets的区别-程序员宅基地

转载自:Gets和scanf的区别char s[20]; gets(s); puts(s);gets与scanf输入字符串的方式也非常类似,但是有几个区别:(1) gets的输入分割符只有回车,因此gets是能够读入空格的。如果输入为"hello world"时,上面程序的运行结果是"hello world"。而如果用scanf则只能输出hello(2) 此外,scanf和g..._scanf和gets的区别

随便推点

mysql sasl_postfix mysql sasl ssl linux 邮件服务器 搭建 笔记_Ja'Soon的博客-程序员宅基地

基本上是按照 http://flurdy.com/docs/postfix 这个教程做的推荐初次搭建邮件服务器的人看的书LINUX系统管理技术手册(第2版)POSTFIX权威指南这2本书里面讲了很多邮件服务器方面的基本概念,不明白的话配置文件的时候简直像猜大小写几点要注意的地方ln -s /tmp/mysql.sock /var/run/mysqld/mysqld.sock配置postfi..._/etc/pki/myca/cacert.pem

Vs2019 植入CUDA代码_vs2019 引入cuda12.2-程序员宅基地

在一个已经在运行的项目代码中植入cuda代码(.cu)文件 步骤:1.If you don't have cuda installed -> 搜索安装cuda。包括配置VS里解决方案或者项目的include,lib目录,包含cuda的对应路径。2.已经有cuda了以后,首先在vs项目里创建 依赖项如果没有列表中没有,点击 “查找现有的”。 在cuda的安装目录下:默认安装在:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\..._vs2019 引入cuda12.2

解决 sublimecodeintel 按回车误打代码的问题-程序员宅基地

1. 问题描述安装完 sublimecodeintel 插件后发现在 for 循环后面输入冒号,会出现 __init__ 推荐补全代码而按回车就自动输入了,QAQ……不过,解决办法还是有的2. 解决办法将下面这行代码放入到 preferences/setting/user 中:"auto_complete_commit_on_tab": true,保存退出再试试输入 for i in range(5): 按下回车就不会自动补全了对了,如果需要自动补全可以在出现提示后按下 Tab

Optical Flow-程序员宅基地

本文作为毕业设计的辅助文档,解释光流法相关的知识。

Sublime Text3手动安装Emmet插件_sublime text3 手工安装emmet-程序员宅基地

1. 到github上下载zip,下载地址:https://github.com/sergeche/emmet-sublime2. 打开sublime text3,preferences -&gt; Browse Packages , 将下载的zip文件解压到弹出的目录下3. 重启 sublime text3 即可使用测试:新建a.html, 输入! 后 按Tab键..._sublime text3 手工安装emmet

推荐文章

热门文章

相关标签