Spring并行执行方法 [英] Spring Parallel Execution Of A Method
问题描述
我处于这样一种情况,我有一个对象集合,每个对象都必须运行耗时约5-10秒才能完成的昂贵方法.
I am in the situation where I have a collection of objects and every object has to run an expensive method that takes about 5-10 seconds to complete.
如何并行运行所有方法并定期检查状态?
How can I run all methods in parallel and check the status periodically?
我尝试将 @Async 批注与 Future 响应一起使用,但未做任何更改.
I tried to use the @Async annotation with a Future response, but nothing changed.
public static void populate(String marketId) {
//irrelevant code removed
List<Company> companies = mongo().find(new Query(c), Company.class);
List<Future> futures = new ArrayList<Future>();
for(Company comp : companies) {
futures.add(comp.updateData(market));
}
}
@Async
public Future<Boolean> updateData(Market market) {
//do my slow logic here
return new AsyncResult(false);
}
Is the ThreadPoolTaskExecutor the way to go?
推荐答案
对于您使用AsyncResult的原因(以及哪种实现... ejb?),我有些困惑. 如果我没记错的话,它就不会那样工作,因为(据我所知)AsyncResult已连接到bean和@Asyncronous批注,从而使来自特定bean方法的响应异步.但是,如果在对象中使用它,则它实际上是顺序的.
I'm a bit confused as to the reasons you use AsyncResult (and which implementation... ejb?). If I'm not mistaken, it will not work that way, as (from what I know) AsyncResult is connected to the bean and @Asyncronous annotation making the response from a specific bean method assyncronous. But if used inside an object, it would be effectively sequential.
您这里需要的是正常的未来.如果是这样,则您需要在执行程序中实际运行这些Future,然后等到调用future.get()直到它们完成. 您可以在这里找到一个不错的教程: http://java.dzone.com/articles/javautilconcurrentfuture
What you need here is a normal Future. If so, you need to actually run these futures in an executor and than wait until they finish by calling future.get(). A nice tutorial on that you can find here: http://java.dzone.com/articles/javautilconcurrentfuture
您也可以研究Akka.演员模型是我个人的最爱,因为您可以简单地产卵一堆工人,告诉他们该怎么做,并在他们完成工作后让他们知道.如果您手头只有一个简单的任务,取决于您的风格,那仍然可能是一个矫kill过正.
You can also look into Akka. Actor model is my personal favourite, as you can simply spawn a bunch of workers, tell them what to do, and let them let you know once they're done with their job. Still it might be an overkill if you only have a simple task at hand, depends on your style.
ExecutorService pool = Executors.newFixedThreadPool(10);
public static void populate(String marketId) {
//irrelevant code removed
List<Company> companies = mongo().find(new Query(c), Company.class);
List<Future> futures = new ArrayList<Future>();
for(Company comp : companies) {
futures.add(comp.updateData(market));
}
for(Future future: futures) {
future.get()
}
}
public Future<Boolean> updateData(Market market) {
return pool.submit(new Callable<Boolean>() {
@Override
public Void call() throws Exception {
//do your slow stuff here;
return false;
}
})
}
当然,如果您需要从这些期货中获得一些实际回报,这是有道理的.如果它们是无效的,并且您只需要一个线程来在其他地方运行某些副作用,那么这样做就没有意义了,您可以简单地使用runnables和executor.类似于以下内容: 等到所有线程在Java中完成工作
of cause this makes sense if you need to get some actual return from those futures. If they're void, and you just need a thread to run some side-effects somewhere else, than there's no point in doing it that way, and you can simply use runnables and executor. Something similar to this: wait until all threads finish their work in java
这篇关于Spring并行执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!