Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哪一个是更好的选择@是否使用ThreadPoolExecutor进行异步?_Java_Spring_Multithreading_Spring Boot_Threadpool - Fatal编程技术网

Java 哪一个是更好的选择@是否使用ThreadPoolExecutor进行异步?

Java 哪一个是更好的选择@是否使用ThreadPoolExecutor进行异步?,java,spring,multithreading,spring-boot,threadpool,Java,Spring,Multithreading,Spring Boot,Threadpool,在spring中的@Async注释中定义手动线程执行器有什么用?当我们不定义executor时,@Async工作得更好 用例: 我已经创建了一个手动线程池,最大池大小为50。如果我们通过200个请求,它最多只能处理50个请求。但如果我们不为@Async定义手动线程执行器,它就可以正常工作 @AsyncmessageQueueExecutor-在最大池大小之后停止 @异步-工作正常 ThreadPoolConfig.java 应用程序属性 手动线程执行器,具有更好的控制,具体到您的应用程序配置 具

在spring中的@Async注释中定义手动线程执行器有什么用?当我们不定义executor时,@Async工作得更好

用例: 我已经创建了一个手动线程池,最大池大小为50。如果我们通过200个请求,它最多只能处理50个请求。但如果我们不为@Async定义手动线程执行器,它就可以正常工作

@AsyncmessageQueueExecutor-在最大池大小之后停止

@异步-工作正常

ThreadPoolConfig.java 应用程序属性
手动线程执行器,具有更好的控制,具体到您的应用程序配置

具体来说,对于线程数,由于您已配置最大池大小为50,执行器将不允许超过50。那太清楚了


但是,除了最大池大小的配置外,这还受到系统处理器的限制。

向我们展示manualThreadExecutor的bean配置。没有它,它将创建无限数量的线程,而不是重用池,这可能会导致各种错误。此外,它将只同时处理50个线程,其他所有的线程都将被放入队列中,一旦线程准备就绪,它就会被处理。我已经用线程池配置更新了我的问题。
package config;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@PropertySource("classpath:threadpool-config.yml")
public class ThreadPoolConfig {

    /* Message Queue Thread Pool */
    /* start */
    @Value("${task.execution.pool.message-queue.core-size}")
    private int mqCorePoolSize;
    @Value("${task.execution.pool.message-queue.max-size}")
    private int mqMaxPoolSize;
    @Value("${task.execution.pool.message-queue.queue-capacity}")
    private int mqQueueCapacity;
    @Value("${task.execution.pool.message-queue.keep-alive}")
    private int mqKeepAliveSeconds;
    @Value("${task.execution.pool.message-queue.allow-core-thread-timeout}")
    private boolean mqAllowCoreThreadTimeOut;
    @Value("${task.execution.pool.message-queue.name}")
    private String mqThreadName;

    @Bean("messageQueueExecutor")
    public Executor messageQueueExecutor() {
        return threadPoolTaskExecutor(mqMaxPoolSize, mqMaxPoolSize, mqQueueCapacity, mqKeepAliveSeconds, mqAllowCoreThreadTimeOut, mqThreadName);
    }
    /* end */

    private ThreadPoolTaskExecutor threadPoolTaskExecutor(int corePoolSize, int maxPoolSize, 
            int queueCapacity, int keepAliveSeconds, 
            boolean allowCoreThreadTimeOut, String threadName) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
        threadPoolTaskExecutor.setThreadNamePrefix(threadName);
        threadPoolTaskExecutor.initialize();
        threadPoolTaskExecutor.setRejectedExecutionHandler((runnable, executor) -> {
            executor.execute(runnable);
        });
        return threadPoolTaskExecutor;
    }   

}
task.execution.pool.message-queue.max-size: 42
task.execution.pool.message-queue.allow-core-thread-timeout: true
task.execution.pool.message-queue.core-size: 7
task.execution.pool.message-queue.keep-alive: 60
task.execution.pool.message-queue.queue-capacity: 11
task.execution.pool.message-queue.name: messagequeue-threadpool-