我在生产中遇到了一个有趣的问题。
我有以下ScheduledThreadPool
分配代码:
ScheduledExecutorService executorService =
Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1);
线程池正在定期处理队列中的某些任务。直到将服务部署在单核环境上的那一刻,一切都运行良好。显然,上面的行转换为:
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(0);
从那时起,JVM进程的CPU利用率一直稳定在100%左右。我将Runtime.getRuntime().availableProcessors() - 1
更改为常量1
的那一刻,问题就解决了。
花了一些时间才找出根本原因,但我仍然不知道其背后的原因。 ScheduledExecutorService
JavaDoc指出:
/**
* Creates a thread pool that can schedule commands to run after a
* given delay, or to execute periodically.
* @param corePoolSize the number of threads to keep in the pool,
* even if they are idle
* @return a newly created scheduled thread pool
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
基本上,0(零)是线程池实例化的有效参数,但在此值下它很奇怪。
可以请人解释为什么吗?
import java.util.Queue;
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) throws InterruptedException {
MessageTaskExecutor asyncEmailGatewayTaskExecutor = new MessageTaskExecutor();
// Infinitely add new tasks to the queue every second
for (int i = 1; ; i++) {
System.out.println(String.format("Adding message #%s to the queue", i));
asyncEmailGatewayTaskExecutor.putMessageIntoQueue(i);
Thread.sleep(1_000);
}
}
static class MessageTaskExecutor {
static final int INITIAL_DELAY_SECONDS = 1;
static final int PROCESSING_RATE_MILLISECONDS = 5_000;
final Queue<Runnable> messageQueue = new ArrayBlockingQueue<>(1_000_000);
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(0);
MessageTaskExecutor() {
// Scavenging Message Tasks Queue every 'PROCESSING_RATE_MILLISECONDS'. Initial delay is fixed for 'INITIAL_DELAY_SECONDS'
executorService.schedule(this::processEmailTasks, INITIAL_DELAY_SECONDS, TimeUnit.SECONDS);
}
void putMessageIntoQueue(int messageId) {
Runnable messageTask = () -> System.out.println(String.format("Message #%s is getting processed!", messageId));
messageQueue.offer(messageTask);
}
void processEmailTasks() {
System.out.println(String.format("There are %s messages in the queue. Processing the messages...", messageQueue.size()));
// Processing messages queue
while (!messageQueue.isEmpty()) {
executorService.submit(messageQueue.poll()); // Submitting task to executor service
}
// Re-scheduling processing job
executorService.schedule(this::processEmailTasks, PROCESSING_RATE_MILLISECONDS, TimeUnit.MILLISECONDS);
}
}
}
此代码分配〜30 MB ,并且JVM进程在单核虚拟机上消耗〜 100%CPU (在 Win 7 / CentOS 7 < / strong>)。 JDK 1.8.0.181 。
通过更改字段:
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(0);
收件人:
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
CPU消耗降低到正常的3-5%。
答案 0 :(得分:5)
这是一个已知的错误:JDK-8129861。它已在JDK 9中修复。
解决方法是将核心池大小设置为至少1:
int corePoolSize = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);