/**
* @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");
});
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2021-05-16
  • 2021-08-24
  • 2021-11-22
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2021-11-22
  • 2022-02-13
  • 2022-12-23
  • 2021-12-03
  • 2021-10-15
相关资源
相似解决方案