java spring 并行调用,Spring并行执行方法

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?

I tried to use the @Async annotation with a Future response, but nothing changed.

public static void populate(String marketId) {

//irrelevant code removed

List companies = mongo().find(new Query(c), Company.class);

List futures = new ArrayList();

for(Company comp : companies) {

futures.add(comp.updateData(market));

}

}

@Async

public Future updateData(Market market) {

//do my slow logic here

return new AsyncResult(false);

}

解决方案

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.

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

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 companies = mongo().find(new Query(c), Company.class);

List futures = new ArrayList();

for(Company comp : companies) {

futures.add(comp.updateData(market));

}

for(Future future: futures) {

future.get()

}

}

public Future updateData(Market market) {

return pool.submit(new Callable() {

@Override

public Void call() throws Exception {

//do your slow stuff here;

return false;

}

})

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值