Java 异步操作的结果列表
我的目标是从10个(或其他任意数量的)异步操作中获得一个结果列表 我正在使用Java 异步操作的结果列表,java,asynchronous,concurrency,guava,Java,Asynchronous,Concurrency,Guava,我的目标是从10个(或其他任意数量的)异步操作中获得一个结果列表 我正在使用com.google.guava来实现并发实用程序,如果有人能为我指出正确的方向,我将不胜感激 在本例中,我试图获得一个成功炸弹列表(炸弹几乎是一个空对象,但在创建时有随机概率抛出问题,以模拟服务调用执行的问题) listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10)); 列出炸弹清单; 未来成功的炸弹;
com.google.guava
来实现并发实用程序,如果有人能为我指出正确的方向,我将不胜感激
在本例中,我试图获得一个成功炸弹列表
(炸弹
几乎是一个空对象,但在创建时有随机概率抛出问题,以模拟服务调用执行的问题)
listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10));
列出炸弹清单;
未来成功的炸弹;
编辑:
这是我到目前为止所想到的,但是列表是空的,尽管它应该有一些成功的元素。。。我不太明白为什么
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
List<ListenableFuture<Bomb>> bombs = new ArrayList<ListenableFuture<Bomb>>();
for(int i=0;i<10;i++)
{
ListenableFuture<Bomb> bomb = service.submit(new Callable<Bomb>(){
public Bomb call() throws Problem
{
return craftBomb();
}
});
}
ListenableFuture<List<Bomb>> successfulBombs = Futures.successfulAsList(bombs);
Futures.addCallback(successfulBombs, new FutureCallback<List<Bomb>>(){
public void onSuccess(List<Bomb> bombs)
{
System.out.println("My successful bombs");
for(Bomb b : bombs)
{
System.out.println(b);
}
}
public void onFailure(Throwable thrown)
{
System.err.println("There was a problem making this bomb.");
}
});
listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10));
List bombs=new ArrayList();
对于(int i=0;i您正在寻找吗?可能与?结合使用,列表是空的,因为您从未向炸弹添加任何内容。您正在将一个空列表传递给期货。successfulAsList
工作解决方案如下
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
List<ListenableFuture<Bomb>> bombs = new ArrayList<ListenableFuture<Bomb>>();
for(int i=0;i<10;i++)
{
ListenableFuture<Bomb> bomb = service.submit(new Callable<Bomb>(){
public Bomb call() throws Problem
{
return craftBomb();
}
});
bombs.add(bomb);
}
ListenableFuture<List<Bomb>> successfulBombs = Futures.successfulAsList(bombs);
Futures.addCallback(successfulBombs, new FutureCallback<List<Bomb>>(){
public void onSuccess(List<Bomb> bombs)
{
System.out.println("My successful bombs");
for(Bomb b : bombs)
{
System.out.println(b);
}
}
public void onFailure(Throwable thrown)
{
System.err.println("There was a problem making this bomb.");
}
});
listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10));
List bombs=new ArrayList();
对于(int i=0;我想你可能找到了什么,让我试一试。我在看successfulAsList()
Futures。successfulAsList()
对你来说更好,因为有些炸弹可能会抛出异常。使用Futures.allAsList()
,一个失败的未来将导致可列出的未来
失败。成功了!炸弹。添加(炸弹)
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
List<ListenableFuture<Bomb>> bombs = new ArrayList<ListenableFuture<Bomb>>();
for(int i=0;i<10;i++)
{
ListenableFuture<Bomb> bomb = service.submit(new Callable<Bomb>(){
public Bomb call() throws Problem
{
return craftBomb();
}
});
bombs.add(bomb);
}
ListenableFuture<List<Bomb>> successfulBombs = Futures.successfulAsList(bombs);
Futures.addCallback(successfulBombs, new FutureCallback<List<Bomb>>(){
public void onSuccess(List<Bomb> bombs)
{
System.out.println("My successful bombs");
for(Bomb b : bombs)
{
System.out.println(b);
}
}
public void onFailure(Throwable thrown)
{
System.err.println("There was a problem making this bomb.");
}
});