37

My question is very similar to this one : @Async prevent a thread to continue until other thread have finished

Basically i need run ~ hundreds of computations in more threads. I want to run only some amount of parallel threads e.g. 5 threads with 5 computationis in paralell.

I am using spring framework and @Async option is natural choice. I do not need full-featured JMS queue, that`s a bit overhead for me.

Any ideas ? Thank you

Community
  • 1
  • 1
Martin V.
  • 3,560
  • 6
  • 31
  • 47

3 Answers3

46

If you are using Spring's Java-configuration, your config class needs to implements AsyncConfigurer:

@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {

    [...]

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("MyExecutor-");
        executor.initialize();
        return executor;
    }
}

See @EnableAsync documentation for more details : http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

Siggen
  • 2,147
  • 1
  • 19
  • 19
  • what if I need multiple asyncs? – Dejell Apr 21 '15 at 17:20
  • 1
    You may annotate several methods with `@Async` and they will share the thread pool / executor defined with `@EnableAsync` config. – Siggen Apr 23 '15 at 10:29
  • 1
    I mean - several executors with different configurations for each – Dejell Apr 23 '15 at 13:47
  • I have never done it, but I think it is possible by defining your own annotation (e.g. `@MyAsync1` and `@MyAsync2`) and then using two `@EnableAsync` (on two distinct configuration classes) while specifying the `annotation` parameter : `@EnableAsync(annotation = @MyAsync1.class)` and `@EnableAsync(annotation = @MyAsync2.class)`. See [documentation](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html#annotation--) of the `@EnableAsync` class – Siggen Apr 24 '15 at 14:15
  • 13
    for having multiple executors with different configurations, you can have a look link : http://www.baeldung.com/spring-async. The section 4.1 tells you how to name the executor and use it like @Async("") – Anoop Isaac Sep 08 '15 at 05:40
  • 1
    If I set queue capacity to 50 which is full and suppose my core pool size threads are not complete, what happens to my 51st task? will it get rejected or will the Async method caller wait? – KCK May 07 '19 at 07:50
  • @KCK It is general thread pool functionality, you can read more on that here for example https://www.baeldung.com/java-threadpooltaskexecutor-core-vs-max-poolsize – user2105282 Aug 18 '23 at 16:03
20

Have you checked out Task Executor? You can define a Thread Pool, with a maximum number of threads to execute your tasks.

If you want to use it with @Async, use this in your spring-config:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

<task:executor id="myExecutor" pool-size="5"/>

<task:scheduler id="myScheduler" pool-size="10"/>

Full reference here (25.5.3). Hope this helps.

jelies
  • 9,110
  • 5
  • 50
  • 65
16

Since spring boot 2.1 you can use auto configuration and change the maximum number of threads in the application properties file

spring.task.execution.pool.max-size=4

See the full documentation:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-task-execution-scheduling

RenRen
  • 10,550
  • 4
  • 37
  • 39