/**
* @desc: 网路异步请求,查数据库 示例
* @author: 毛会懂
* @create: 2022-02-08 15:14:00
**/
public class Test5Main {
public static void main(String[] args) {
// 假设参数为userId,根据userId查询有相同爱好的人
String userId = "00001";


// 根据userId 查userId的兴趣爱好
CompletableFuture<List<String>> future = getHobbyByUserId(userId)
.thenCompose(hobby -> getUsersByHobby(hobby));
try {
List<String> result = future.get();
System.out.println("返回的结果是:" + result);
}catch (Exception ex){

}
}


// 异步查询userId的兴趣爱好
private static CompletableFuture<String> getHobbyByUserId(String userId) {
return CompletableFuture.supplyAsync(() ->{
// 根据userId查询数据库
return "爱好1";
});
}

private static CompletableFuture<List<String>> getUsersByHobby(String hobby){
return CompletableFuture.supplyAsync(() -> {
// 根据hobby查询数据库
return Arrays.asList("姓名1","姓名2");
});
}
posted on 2022-02-08 15:34  毛会懂  阅读(315)  评论(0编辑  收藏  举报