newFixedThreadPool() vs newCachedThreadPool() [duplicate]

倖福魔咒の 提交于 2019-12-07 14:37:26

问题


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.

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.


回答1:


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());

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



来源:https://stackoverflow.com/questions/29620591/newfixedthreadpool-vs-newcachedthreadpool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!