Java 8 主completablefuture完成后如何并发调用多个completablefuture

Java 8 主completablefuture完成后如何并发调用多个completablefuture,java-8,completable-future,Java 8,Completable Future,如何增强下面的函数,其中getB、getC、getD与A有依赖关系,需要等待A完成调用。 不过,我希望在完成一项调查后,同时致电B C D 谢谢你的帮助 注意:所有dbService都返回一个完整的未来 CompletableFuture<List<A>> a= dbService.getA(request); a.thenApply(a-> { try { return dbService.g

如何增强下面的函数,其中getB、getC、getD与A有依赖关系,需要等待A完成调用。 不过,我希望在完成一项调查后,同时致电B C D

谢谢你的帮助

注意:所有dbService都返回一个完整的未来

CompletableFuture<List<A>> a= dbService.getA(request);
a.thenApply(a-> {
                try {
                    return dbService.getB(a);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            })
            .thenAccept (result->{
                //do something with B
            });

a.thenApply(a-> {
                try {
                    return dbService.getC(a);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            })
            .thenAccept (result->{
                //do something with C
            });

a.thenApply(a-> {
                try {
                    return dbService.getD(a);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            })
            .thenAccept (result->{
                //do something with D
            });

CompletableFuture a=dbService.getA(请求);
a、 然后应用(a->{
试一试{
返回dbService.getB(a);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
})
。然后接受(结果->{
//和B做点什么
});
a、 然后应用(a->{
试一试{
返回dbService.getC(a);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
})
。然后接受(结果->{
//用C做点什么
});
a、 然后应用(a->{
试一试{
返回dbService.getD(a);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
})
。然后接受(结果->{
//用D做点什么
});

如果您不关心B、C、D的返回,那么:

CompletionStage<List<A>> aFuture = dbService.getA(request);
aFuture.whenCompleteAsync((a, ex) -> {
    if (ex != null) {
        dbService.getB(a);
        // ...
    } else {
        // ...
    }
});
aFuture.whenCompleteAsync((a, ex) -> {
    if (ex != null) {
        dbService.getC(a);
        // ...
    } else {
        // ...
    }
});
aFuture.whenCompleteAsync((a, ex) -> {
    if (ex != null) {
        dbService.getD(a);
        // ...
    } else {
        // ...
    }
});
您可以使用
thenCombine
,将其缩短,这类似于等待2个期货的
thenApply

CompletionStage<List<A>> aFuture = dbService.getA(request);

CompletionStage<B> bFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getB(a);
});
CompletionStage<C> cFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getC(a);
});
CompletionStage<D> dFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getD(a);
});

CompletionStage<Something> sFuture = bFuture.thenCompose(b -> {
    cFuture.thenCombine(dFuture, (c, d) -> {
        // do something with b, c, d
        return new Something();
    });
});
完成阶段{
返回dbService.getB(a);
});
CompletionStage cFuture=a未来。然后应用同步(a->{
返回dbService.getC(a);
});
CompletionStage dFuture=a未来。然后应用同步(a->{
返回dbService.getD(a);
});
CompletionStage sFuture=CompletableFutures.combine((bFuture,cFuture,dFuture)->{
//用b,c,d做点什么
返回新事物();
}

使用
然后应用同步
?你的意思是将所有功能与应用同步结合起来?如果是,我如何在参数A中重新通过?你的意思是什么?只需将所有三个
然后应用
替换为
然后应用同步
。我的意思是有没有办法将所有三个功能结合在一起?谢谢你说的,你希望B、C和D同时运行。所以不,你可以没有在一个函数中同时运行这些调用。这也没有任何意义。如果您想简化代码,请修复dbService方法,使其不抛出
InterruptedException
,因为返回表示异步操作的对象,但声明抛出表示异步操作的异常是矛盾的等待操作。
CompletionStage<List<A>> aFuture = dbService.getA(request);

CompletionStage<B> bFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getB(a);
});
CompletionStage<C> cFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getC(a);
});
CompletionStage<D> dFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getD(a);
});

CompletionStage<Something> sFuture = bFuture.thenCompose(b -> {
    cFuture.thenCombine(dFuture, (c, d) -> {
        // do something with b, c, d
        return new Something();
    });
});
CompletionStage<List<A>> aFuture = dbService.getA(request);

CompletionStage<B> bFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getB(a);
});
CompletionStage<C> cFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getC(a);
});
CompletionStage<D> dFuture = aFuture.thenApplyAsync(a -> {
    return dbService.getD(a);
});

CompletionStage<Something> sFuture = CompletableFutures.combine((bFuture, cFuture, dFuture) -> {
    // do something with b, c, d
    return new Something();
}