newFixedThreadPool()vs newCachedThreadPool() [英] newFixedThreadPool() vs newCachedThreadPool()

查看:258
本文介绍了newFixedThreadPool()vs newCachedThreadPool()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 newCachedThreadPool()根据需要创建一个线程池,根据需要创建新线程,但会在可用时重用先前构造的线程,而在<$的情况下c $ c> newFixedThreadPool(int size)指定用于创建指定大小的线程池的大小。

In case newCachedThreadPool() as per creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available whereas in case of newFixedThreadPool(int size) specify size to create the thread pool with size specified.

为什么不是 newFixedThreadPool(int size) newCachedThreadPool()中实现时尚,其中线程池仅在需要时创建新线程并将线程限制为大小?

Why isn'tnewFixedThreadPool(int size) implemented in newCachedThreadPool() fashion where thread pool creates the new thread only when required and will limit the thread to size?

上述任何澄清都非常有用。

Any clarrification on the above is really helpful.

推荐答案

newFixedThreadPool也懒惰地创建线程,试试这个测试

newFixedThreadPool also creates threads lazily, try this test

    ThreadPoolExecutor p = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
    System.out.println(p.getPoolSize());
    p.execute(new Runnable() {public void run() {}});
    System.out.println(p.getPoolSize());

不同之处在于a)FixedThreadPool的线程永不过期,而CacheThreadPool在上次使用后的60秒内到期)CacheThreadPool活动的最大活动线程是无限的

The difference is that a) FixedThreadPool's threads never expire, while CacheThreadPool expire in 60 secs after last used b) CacheThreadPool active max active threads is unlimited

这篇关于newFixedThreadPool()vs newCachedThreadPool()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆