哪个更好的选择? @Async是否带有ThreadPoolExecutor? [英] Which is better option? @Async with or without ThreadPoolExecutor?

查看:497
本文介绍了哪个更好的选择? @Async是否带有ThreadPoolExecutor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring的@Async批注中定义手动线程执行程序有什么用?当我们不定义执行程序时,@ Async会更好地工作.

What is the use of defining manual thread executor inside the @Async annotation in spring? When we don't define executor, the @Async is working better.

用例: 我创建了一个手动线程池,最大线程池大小为50.如果传递200个请求,则最多只能处理50个请求.但是,如果我们没有为@Async定义手动线程执行程序,它就可以正常工作.

Use case: I have created a manual thread pool with max pool size is 50. If we pass 200 requests it process only up to 50 requests. But if we don't define manual thread executor for @Async, it is working fine.

@Async("messageQueueExecutor")-在最大池大小后停止

@Async("messageQueueExecutor") - Stops after max pool size

@Async-正常工作

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;
    }   

}

application.properties

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-

推荐答案

手动线程执行程序,可以针对您的应用程序配置进行更好的控制.

Manual Thread Executor, to have a better control, specific to your application configuration.

具体来说,由于已配置线程的最大池大小为50,执行程序将不允许超过50.这非常清楚!

In specific, to the number of threads, since you have configured, max pool size of 50, the executor WILL NOT allow more than 50. Thats crystal clear!

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

However, the apart from your configuration for max pool size, this is also limited by the System processor.

这篇关于哪个更好的选择? @Async是否带有ThreadPoolExecutor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆