具有corePoolSize = 0的ScheduledThreadPoolExecutor会在一个CPU核心上导致100%负载 [英] ScheduledThreadPoolExecutor with corePoolSize = 0 causes 100% load on one CPU core
问题描述
给出以下 <的配置code> ScheduledThreadPoolExecutor ,每五秒运行一次简单的任务:
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);
在Oracle JRE上 1.8.0_66
有一个由 ScheduledThreadPoolExecutor
创建的线程,它不断导致一个CPU核心100%负载。
调查线程转储显示以下stacktrace:
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)
使用 corePoolSize = 1
池中仍有一个线程。但是,该线程基本上始终处于状态 TIMED_WAITING
并因此空闲。
With corePoolSize = 1
there is still one thread in the pool. However, the thread is basically always in state TIMED_WAITING
and thus idle.
是否行为 ScheduledThreadPoolExecutor
with corePoolSize = 0
已知功能,未经验证的错误配置甚至是错误?
Is the behavior of ScheduledThreadPoolExecutor
with corePoolSize = 0
a known feature, an unvalidated misconfiguration or even a bug?
推荐答案
看起来你已经点击 JDK已在Java 9中修复的-8129861 。它也可能与 JDK有关-8022642 。
It looks like you have hit JDK-8129861 that has been fixed in Java 9. It may also be related to JDK-8022642.
这篇关于具有corePoolSize = 0的ScheduledThreadPoolExecutor会在一个CPU核心上导致100%负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!