This question already has an answer here:
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.
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