Java PostalableFuture-多线程问题
我是多线程的新手,对此感到困惑。
我正在尝试创建两个并行线程,在每个线程中,它首先调用API-1并获取响应,然后使用该响应来调用第二个API-2并获得最终结果。
第一个版本代码类似于:
CompletableFuture<List<String>> listFuture1 = CompletableFuture
.supplyAsync(() -> {
List<String> res = new ArrayList<>();
try {
res = callAPI1(id);
return res;
} catch(Exception e) {
//...
}
return res;
});
CompletableFuture<List<String>> listFuture2 = CompletableFuture
.supplyAsync(() -> {
List<String> res = new ArrayList<>();
try {
res = callFirstAPI2(id);
return res;
} catch(Exception e) {
//...
}
return res;
});
List<String> list1 = new ArrayList<>(listFuture1.join());
List<String> list2 = new ArrayList<>(listFuture2.join());
List<String> res1 = callAPI2(list1);
List<String> res2 = callAPI2(list2);
以上代码正常工作,但是获得最终结果非常慢,并且看起来CallFirstapi1()和Callfirstapi2()在同一线程中没有发生。
然后我尝试了这种方式:
public List<String> getList(int id) {
CompletableFuture<List<String>> res = CompletableFuture
.supplyAsync(() -> {
List<String> list = new ArrayList<>();
try {
list= callAPI1(id);
return list;
} catch(Exception e) {
}
return list;
}).thenApply(list -> {
List<String> res = new ArrayList<>();
try {
res= callAPI2(list);
return res;
} catch(Exception e) {
}
return res;
});
return new ArrayList<>(res.join());
}
public static void main(String args[]) {
List<String> res1 = getList(id1);
List<String> res2 = getList(id2);
}
现在结果全部为空。 我进行了调试,发现在当时的Apply()中,列表为空,这就是为什么最终结果为空的原因。因此,即使没有完成,即使无法完成。
如何更正代码?
I am new to multithreading and feel confused about it.
I am trying to create two parallel threads, in each thread, it first calls an api-1 and get the response, then use that response to call the second api-2 and get the final result.
The first version code is like:
CompletableFuture<List<String>> listFuture1 = CompletableFuture
.supplyAsync(() -> {
List<String> res = new ArrayList<>();
try {
res = callAPI1(id);
return res;
} catch(Exception e) {
//...
}
return res;
});
CompletableFuture<List<String>> listFuture2 = CompletableFuture
.supplyAsync(() -> {
List<String> res = new ArrayList<>();
try {
res = callFirstAPI2(id);
return res;
} catch(Exception e) {
//...
}
return res;
});
List<String> list1 = new ArrayList<>(listFuture1.join());
List<String> list2 = new ArrayList<>(listFuture2.join());
List<String> res1 = callAPI2(list1);
List<String> res2 = callAPI2(list2);
The above code works correctly but it is very slow to get the final result, and looks like the callFirstAPI1() and callFirstAPI2() are not happening in the same thread.
Then I tried this way:
public List<String> getList(int id) {
CompletableFuture<List<String>> res = CompletableFuture
.supplyAsync(() -> {
List<String> list = new ArrayList<>();
try {
list= callAPI1(id);
return list;
} catch(Exception e) {
}
return list;
}).thenApply(list -> {
List<String> res = new ArrayList<>();
try {
res= callAPI2(list);
return res;
} catch(Exception e) {
}
return res;
});
return new ArrayList<>(res.join());
}
public static void main(String args[]) {
List<String> res1 = getList(id1);
List<String> res2 = getList(id2);
}
Now the results are all empty.
I debugged and found that in the thenApply(), the list is empty, that's why the final result is empty. so thenApply() is still executed even the completablefuture is not completed.
How to correct the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论