Given is the following configuration of a ScheduledThreadPoolExecutor
that runs a simple task every five seconds:
int corePoolSize = 0;
ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(corePoolSize);
Runnable task = () -> System.out.println("XXX");
executor.scheduleAtFixedRate(task, 5, 5, TimeUnit.SECONDS);
On Oracle JRE 1.8.0_66
there’s one thread created by the ScheduledThreadPoolExecutor
that constantly causes 100% load on one CPU core.
Investigating the thread dump reveals the following stacktrace:
"pool-1-thread-1" - Thread t@10
java.lang.Thread.State: RUNNABLE
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.poll(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
With corePoolSize = 1
there is still one thread in the pool. However, the thread is basically always in state TIMED_WAITING
and thus idle.
Is the behavior of ScheduledThreadPoolExecutor
with corePoolSize = 0
a known feature, an unvalidated misconfiguration or even a bug?
It looks like you have hit JDK-8129861 that has been fixed in Java 9. It may also be related to JDK-8022642.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With