正如我们在注释中确定的那样,消耗 CPU 的线程在以下堆栈中正忙:
"pool-9-thread-1" #49 prio=5 os_prio=0 tid=0x00007ffd508e8000 nid=0x3a0c runnable [0x00007ffd188b6000]
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)
我设法通过创建一个 来重现类似的行为,将其配置为允许核心线程超时,并在短延迟的情况下安排许多重复任务。它在我的机器上产生了大量的CPU,并且输出是相似的(有时深入到方法中)。此代码再现它:ScheduledThreadPoolExecutor
jstack
poll
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setKeepAliveTime(1, TimeUnit.MINUTES);
executor.allowCoreThreadTimeOut(true);
for (long i = 0; i < 1000; i++) {
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
}
}, 0, 1, TimeUnit.NANOSECONDS);
}
现在我们只需要确定哪些代码设置了一个 破碎的 .我搜索了RabbitMQ和Netty的源代码,没有找到任何遗漏。这可能是你在自己的代码中做的事情吗?ScheduledThreadPoolExecutor
编辑:如评论中提到的,根本原因是初始化,显然可以导致某些平台的CPU旋转。这是在OP的代码中完成的。ScheduledThreadPoolExecutor
0