ScheduledThreadPoolExecutor with corePoolSize = 0 会导致一个 CPU 内核上的 100% 负载
2022-09-04 20:52:01
给定的是一个 ScheduledThreadPoolExecutor
的以下配置,该配置每五秒运行一个简单任务:
int corePoolSize = 0;
ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(corePoolSize);
Runnable task = () -> System.out.println("XXX");
executor.scheduleAtFixedRate(task, 5, 5, TimeUnit.SECONDS);
在Oracle JRE上,有一个由 创建的线程,该线程不断在一个CPU内核上造成100%的负载。调查线程转储会显示以下堆栈跟踪:1.8.0_66
ScheduledThreadPoolExecutor
"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
具有已知功能的行为是未经验证的错误配置还是错误?ScheduledThreadPoolExecutor
corePoolSize = 0