gpt4 book ai didi

java - ThreadPoolTask​​Executor 抛出 RejectedExecutionException

转载 作者:行者123 更新时间:2023-12-01 18:51:57 24 4
gpt4 key购买 nike

我想实现以下行为:

  1. 从文件中读取 n 个事件
  2. 在线程中处理它们
  3. 如果仍有任何事件,请返回步骤 1

我编写了以下应用程序来测试解决方案,但它在随机时刻失败,例如。

java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@62b3df3a[Running, pool size = 5, active threads = 4, queued tasks = 0, completed tasks = 70]] did not accept task: java.util.concurrent.CompletableFuture$AsyncSupply@71ea1fda

如果我不想将事件放入队列,我应该设置多少队列容量?我想立即处理它们。

我正在使用 Open JDK 11 和 Spring Boot 2.2.2.RELEASE

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

@Autowired
private ThreadPoolTaskExecutor eventExecutor;

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Bean(name = "eventExecutor")
public ThreadPoolTaskExecutor eventExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(5);
pool.setQueueCapacity(0);
pool.setAwaitTerminationSeconds(0);
pool.initialize();
return pool;
}

@Override
public void run(String... args) {
System.out.println("Start events processing");
long start = System.currentTimeMillis();

int result = 0;
for (int i = 0; i < 100; i++) {
List<CompletableFuture<Integer>> completableFutures = getEvents(5).stream()
.map(event -> CompletableFuture.supplyAsync(() -> processEvent(event), eventExecutor))
.collect(Collectors.toList());

result += completableFutures.stream()
.mapToInt(CompletableFuture::join)
.sum();
}

long timeMillis = System.currentTimeMillis() - start;

System.out.println("Took " + timeMillis + "ms, " + result);
}

private List<Event> getEvents(int n) {
List<Event> events = new ArrayList<>();
for (int i = 1; i <= n; i++) {
events.add(new Event(i));
}
return events;
}

private int processEvent(Event event) {
System.out.println("processing event " + event.id);

try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("processing event " + event.id + " finished");

return 1;
}

private static class Event {

private int id;

private Event(int id) {
this.id = id;
}
}
}

最佳答案

我建议更改pool.setQueueCapacity(0)以使用正值,以便在如此配置的固定线程中没有可用线程时允许任务排队等待处理。池大小(corePoolSize == maxPoolSize == 5)。

输出“池大小 = 5, Activity 线程 = 4”显示 active threads大约数量.

理论上,在尝试处理新一批事件之前,线程可能不会返回到池中。

关于java - ThreadPoolTask​​Executor 抛出 RejectedExecutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com