Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScheduledThreadPoolExecutor with corePoolSize = 0 causes 100% load on one CPU core

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?

like image 795
Benedikt Waldvogel Avatar asked Jan 05 '16 15:01

Benedikt Waldvogel


1 Answers

It looks like you have hit JDK-8129861 that has been fixed in Java 9. It may also be related to JDK-8022642.

like image 166
assylias Avatar answered Sep 29 '22 03:09

assylias