java - ThreadPoolTask​​Executor 抛出 RejectedExecutionException

标签 java spring spring-boot java.util.concurrent completable-future

我想实现以下行为:

  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/

相关文章:

java - spring-ws 如何给请求体中的soap action 添加一个属性

java - Spring Boot 应用程序无法使用 Spring Boot Dashboard 在 Eclipse 中启动

java - 转换 JScrollPane

java - JBOSS Struts 2 应用程序入侵

java - 自定义表单选择中的项目

json - 消除重复的Json元素并检索以大写字母开头的元素名称spring boot/java

java - Spring Boot - 使配置最终化

java - 为什么我们可以将 Java 接口(interface)转换为*任何*非最终类?

java - 即将被收集时如何获取java对象本身

linux -/usr/bin/ld : error: cannot find -larchives