Spring @Async 限制线程数 [英] Spring @Async limit number of threads

查看:40
本文介绍了Spring @Async 限制线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

有什么想法吗?谢谢

推荐答案

您是否检查了 Task Executor?您可以定义一个线程池,用最大数量的线程来执行您的任务.

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

如果您想将它与 @Async 一起使用,请在您的 spring-config 中使用它:

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"/>

完整参考这里 (25.5.3).希望这会有所帮助.

Full reference here (25.5.3). Hope this helps.

这篇关于Spring @Async 限制线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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