Reputation: 360046
I'm using Executors.newScheduledThreadPool()
to create a ScheduledExecutorService
, specifying the number of threads like so:
int corePoolSize = 42;
ScheduledExecutorService foo = Executors.newScheduledThreadPool(corePoolSize);
According to the JavaDocs, the corePoolSize
argument sets
the number of threads to keep in the pool, even if they are idle.
Does this mean that this ExecutorService
implementation may create more than corePoolSize
threads as needed, similar to a cached thread pool?
Upvotes: 16
Views: 12452
Reputation: 304
No. The correct answer is no, a ScheduledExecutorService will not spawn new threads.
Upvotes: 14
Reputation: 134340
Does this mean that this ExecutorService implementation may create more than
corePoolSize
threads as needed?
Yes, that is exactly what it means. The reason for the existence of corePoolSize
is the expense of thread-creation. If you expect to be firing large numbers of short-lived tasks at your executor service, you may expect to find, at a given point in time, large numbers of idle threads.
Rather than have these threads die, only to be replaced a very short time later by newly-created threads, corePoolSize
will ensure that there will always be a number of spinning threads.
Upvotes: 6