'What are the defaults in Spring @Async?

Could you tell me what are the default parameters for Spring @Async ThreadPoolTaskExecutor or how can I find them one my own?

What are the default values for maxPoolSize, corePoolSize, and queueCapcity?

Should I override them to improve my application or is it just fine to use default values?



Solution 1:[1]

Regarding ThreadPoolTaskExecutor's implementation. You can check it at their github repository. ThreadPoolTaskExecutor

private int corePoolSize = 1;

private int maxPoolSize = Integer.MAX_VALUE;

private int queueCapacity = Integer.MAX_VALUE;

Solution 2:[2]

I assume you would like to use @EnableAsync (javadoc) annotation to support async tasks execution in spring.

In this case the documentation states the following:

By default, Spring will be searching for an associated thread pool definition: either unique org.springframework.core.task.TaskExecutor bean in the context, or an java.util.concurrent.Executor bean named "taskExecutor" otherwise.

If neither of the two is resolvable, a org.springframework.core.task.SimpleAsyncTaskExecutor will be used to process async method invocations.

Now if you want to provide your own customization, you can define (implement) an AsyncConfigurer (javadoc) that basically allows to define an executor and exception handler (out of scope for this question).

Solution 3:[3]

I think you need @EnableAsync to enable @Async annotation and this annotation will use default implementation SimpleAsyncTaskExecutor

SimpleAsyncTaskExecutor implementation does not reuse any threads, rather it starts up a new thread for each invocation. However, it does support a concurrency limit which will block any invocations that are over the limit until a slot has been freed up.

You can define your own ThreadPoolTaskExecutor like

@Configuration
public class ThreadConfig {

    @Bean("otherExecutor")
    public TaskExecutor threadPoolTaskExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(16);
        executor.setMaxPoolSize(32);
        executor.initialize();

        return executor;
    }
}

And refer to this in the @Async

@Async("otherExecutor")
void doSomething(String s) {
    // this will be executed asynchronously by "otherExecutor"
}

Solution 4:[4]

According to Spring sources @EnableAsync annotation configures acctually SimpleAsyncTaskExecutor and that doesn’t reuse threads and the number of threads used at any time aren’t limited by default.

There's a queue between that process which submits jobs and the thread pool. If all threads are occupied, the job will just be queued. If the queue is full and the threads are also occupied, then the new task will be rejected. There are couple of rejection policies you can choose (for example. caller runs).

If you are looking for true pooling look at SimpleThreadPoolTaskExecutor and ThreadPoolTaskExecutor

Solution 5:[5]

The bean name for the task executor that @Async uses is applicationTaskExecutor

The properties for applicationTaskExecutor are defined in TaskExecutionProperties

private int queueCapacity = Integer.MAX_VALUE;
private int coreSize = 8;
private int maxSize = Integer.MAX_VALUE;

Solution 6:[6]

Defined in TaskExecutionProperties. Autoconfigure uses this file instead of ThreadPoolTaskExecutor as mentioned in other answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mark Bramnik
Solution 3 sendon1982
Solution 4 mmalkiew
Solution 5 Grigory Zhadko
Solution 6 Dileep