Java PostalableFuture-多线程问题

发布于 2025-02-06 06:32:03 字数 1951 浏览 0 评论 0原文

我是多线程的新手,对此感到困惑。

我正在尝试创建两个并行线程,在每个线程中,它首先调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文