Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 异步操作的结果列表_Java_Asynchronous_Concurrency_Guava - Fatal编程技术网

Java 异步操作的结果列表

Java 异步操作的结果列表,java,asynchronous,concurrency,guava,Java,Asynchronous,Concurrency,Guava,我的目标是从10个(或其他任意数量的)异步操作中获得一个结果列表 我正在使用com.google.guava来实现并发实用程序,如果有人能为我指出正确的方向,我将不胜感激 在本例中,我试图获得一个成功炸弹列表(炸弹几乎是一个空对象,但在创建时有随机概率抛出问题,以模拟服务调用执行的问题) listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10)); 列出炸弹清单; 未来成功的炸弹;

我的目标是从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.");
        }
    });