Spring @Async 限制线程数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13206792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:29:24  来源:igfitidea点击:

Spring @Async limit number of threads

multithreadingspringasynchronousthread-synchronization

提问by Martin V.

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

我的问题与这个问题非常相似:@Async 阻止一个线程继续,直到其他线程完成

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.

基本上我需要在更多线程中运行~数百次计算。我只想运行一些并行线程,例如 5 个并行计算的 5 个线程。

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.

我正在使用 spring 框架,@Async 选项是自然的选择。我不需要全功能的 JMS 队列,这对我来说有点开销。

Any ideas ? Thank you

有任何想法吗 ?谢谢

采纳答案by jelies

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

你退房了Task Executor吗?你可以定义一个线程池,用最大数量的线程来执行你的任务。

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

如果要与 一起@Async使用,请在 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.

完整参考这里(25.5.3)。希望这可以帮助。

回答by Siggen

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

如果您使用的是 Spring 的 Java 配置,则您的配置类需要实现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 @EnableAsyncdocumentation for more details : http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

有关@EnableAsync更多详细信息,请参阅文档:http: //docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

回答by RenRen

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

从 spring boot 2.1 开始,您可以使用自动配置并更改应用程序属性文件中的最大线程数

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

查看完整文档:https:
//docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-task-execution-scheduling