java-异步调用

86 阅读1分钟

异步调用 image.png 多线程掉接口

image.png

public UserInfo getUserInfo(Long id) throws InterruptedException, ExecutionException {

final UserInfo userInfo = new UserInfo();

CompletableFuture userFuture = CompletableFuture.supplyAsync(() -> {

getRemoteUserAndFill(id, userInfo);

return Boolean.TRUE;

}, executor);

CompletableFuture bonusFuture = CompletableFuture.supplyAsync(() -> {

getRemoteBonusAndFill(id, userInfo);

return Boolean.TRUE;

}, executor);

CompletableFuture growthFuture = CompletableFuture.supplyAsync(() -> {

getRemoteGrowthAndFill(id, userInfo);

return Boolean.TRUE;

}, executor);

CompletableFuture.allOf(userFuture, bonusFuture, growthFuture).join();

userFuture.get();

bonusFuture.get();

growthFuture.get();

return userInfo;

}