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

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

    In your first example, subsequent tasks are rejected because the AbortPolicy is the default RejectedExecutionHandler. The ThreadPoolExecutor contains the following policies, which you can change via the setRejectedExecutionHandler method:

    CallerRunsPolicy
    AbortPolicy
    DiscardPolicy
    DiscardOldestPolicy
    

    It sounds like you want cached thread pool with a CallerRunsPolicy.

    0 讨论(0)
提交回复
热议问题