Impossible to make a cached thread pool with a size limit?

前端 未结 13 835
日久生厌
日久生厌 2020-11-28 00:31

It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create.

Here is how static Executors.newCachedThreadPool is

13条回答
  •  爱一瞬间的悲伤
    2020-11-28 01:08

    Unless I've missed something, the solution to the original question is simple. The following code implements the desired behavior as described by the original poster. It will spawn up to 5 threads to work on an unbounded queue and idle threads will terminate after 60 seconds.

    tp = new ThreadPoolExecutor(5, 5, 60, TimeUnit.SECONDS,
                        new LinkedBlockingQueue());
    tp.allowCoreThreadTimeOut(true);
    

提交回复
热议问题