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

前端 未结 13 816
日久生厌
日久生厌 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:27

    This is what you want (atleast I guess so). For an explanation check Jonathan Feinberg answer

    Executors.newFixedThreadPool(int n)

    Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

提交回复
热议问题