我的问题与这个非常相似:@Async 阻止一个线程继续执行,直到其他线程完成 https://stackoverflow.com/questions/4324212/async-prevent-a-thread-to-continue-until-other-thread-have-finished
基本上我需要在更多线程中运行〜数百次计算。我只想运行一定数量的并行线程,例如5 个线程并行 5 个计算。
我正在使用 spring 框架,@Async 选项是自然的选择。我不需要全功能的 JMS 队列,这对我来说有点开销。
有任何想法吗 ?
谢谢
如果您使用 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 @EnableAsync
有关更多详细信息的文档:http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)