Why doesn't ScheduledExecutorService spawn threads as needed?

前端 未结 2 766
心在旅途
心在旅途 2021-01-04 18:16

In my application I use ScheduledExecutorService, but only one thread is spawned to handle the scheduled tasks. Is this because ScheduledExecutorService does not spawn thre

2条回答
  •  不知归路
    2021-01-04 19:16

    From the javadoc for ScheduledThreadPoolExecutor:

    While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run.

    In other words, maximumPoolSize == corePoolSize. You set corePoolSize to 1, so that's all it will spawn.

提交回复
热议问题