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
There is only one thread because you create the thread pool with Executors.newScheduledThreadPool(1)
, which means that the thread pool contains only 1 thread. If you want 10 threads, pass 10 as argument. Note that the documentation of ScheduledThreadPoolExecutor, which is what this method returns, explicitly says that the thread pool has a fixed size.
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.