Which is better option? @Async with or without Thr

2019-10-19 02:10发布

问题:

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.

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") - Stops after max pool size

@Async - works fine

ThreadPoolConfig.java

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-

回答1:

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

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.