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

查看:1550
本文介绍了Spring @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天全站免登陆