FixedThreadPool 与 CachedThreadPool: 两害相权取其轻

我有一个产生线程(约5-150)的程序,它执行一系列任务。最初,我使用 FixedThreadPool是因为 这个类似的问题表明它们更适合于较长的任务,并且由于我对多线程的知识非常有限,我考虑了线程的平均寿命(几分钟)“ 长命百岁”。

但是,我最近增加了产生额外线程的功能,这样做使我超出了设置的线程限制。在这种情况下,最好是猜测并增加我可以允许的线程数,还是切换到 CachedThreadPool,这样我就没有浪费的线程了?

初步试验它们,没有 看起来有什么不同,所以我倾向于使用 CachedThreadPool只是为了避免浪费。但是,线程的生命周期是否意味着我应该选择一个 FixedThreadPool,只处理未使用的线程?这个问题让这些额外的线程看起来没有被浪费,但是我希望得到澄清。

84047 次浏览

A CachedThreadPool seems appropriate for your situation as there are no negative consequence to using one for long running threads directly. The comment in the java doc about CachedThreadPools being suitable for short tasks merely suggest that they are particularly appropriate for such cases, not that they cannot be used for long running tasks.

The main concern with a CachedThreadPool is that it will create up to Integer.MAX_VALUE number of threads as it will always spawn a new thread if an unused one does not exist in the cache. So if you have long running tasks it is then more likely that you could grow the number of concurrent threads more than you desire since this type of thread pool will not limit how many execute concurrently itself. This does not seem to be a problem for your use case as described, but it is something to be aware of.

To elaborate further on the difference between a CachedThreadPool and a FixedThreadPool, Executors.newCachedThreadPool and Executors.newFixedThreadPool are both backed by the same thread pool implementation (at least in the open JDK) via an instance of ThreadPoolExecutor, just with different parameters. The differences just being their thread minimum, maximum, thread kill time, and queue type.

public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}


public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}

A FixedThreadPool does have its advantages when you do in fact want to work with a fixed number of threads, since then you can submit any number of tasks to the executor service while knowing that the number of threads will be maintained at the level you specified. If you explicitly want to grow the number of threads, then this is not the appropriate choice.

This does however mean that the one issue that you may have with the CachedThreadPool is in regards to limiting the number of threads that are running concurrently. The CachedThreadPool will not limit them for you, so you may need to write your own code to ensure that you do not run too many threads, which you can do relatively easily by instantiating your own ThreadPoolExecutor with your desired behaviour characteristics. This really depends on the design of your application and how tasks are submitted to the executor service.

Both FixedThreadPool and CachedThreadPool are evils in highly loaded applications.

CachedThreadPool is more dangerous than FixedThreadPool

If your application is highly loaded & demands low latency, better to get rid of both options due to below drawbacks

  1. Unbounded nature of task queue : It may cause out of memory or high latency
  2. Long running threads will cause CachedThreadPool to go out of control on Thread creation

Since you know that both are evils, lesser evil doesn't do any good. Prefer ThreadPoolExecutor, which provides granular control on many parameters.

  1. Set the task queue as bounded queue to have better control
  2. Have right RejectionHandler - Your own RejectionHandler or Default handlers provided by JDK
  3. If you have something to do on before/after completion of task, override beforeExecute(Thread, Runnable) and afterExecute(Runnable, Throwable)
  4. Override ThreadFactory, if thread customization is required
  5. Control Thread pool size dynamically at run time ( related SE question : Dynamic Thread Pool)

So I have a program that spawns threads (~5-150) which perform a bunch of tasks.

Are you sure you understand how threads are actually processed by your OS and hardware of choice? How Java maps threads to OS threads, how that maps threads to CPU threads etc.? I'm asking because creating 150 threads within in ONE JRE only makes sense if you have massive CPU cores/threads underneath, which most likely is not the case. Depending on the OS and RAM in use, creating more than n threads might even result in your JRE being terminated because of OOM errors. So you should really distinguish between threads and work to do by those threads, how many work you are even able to process etc.

And that's the problem with CachedThreadPool: It doesn't make sense to queue up long running work in threads which actually can't run because you only have 2 CPU cores able to process those threads. If you end up with 150 scheduled threads you might create a lot of unnecessary overhead for the schedulers used within Java and the OS to concurrently process them. This is simply impossible if you only have 2 CPU cores, unless your threads are waiting for I/O or such all the time. But even in that case a lot of threads would create a lot of I/O...

And that problem doesn't occur with FixedThreadPool, created with e.g. 2+n threads, where n is reasonable low of course, because with that hardware and OS resources are used with far less overhead for managing threads which can't run anyway.