等待多个期货的回调 [英] Waiting for callback for multiple futures

查看:39
本文介绍了等待多个期货的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我深入研究了一些使用 API 的工作.API 使用 Unirest http 库来简化从 Web 接收的工作.自然,由于数据是从 API 服务器调用的,我尝试通过对 API 使用异步调用来提高效率.我的想法结构如下:

Recently I've delved into a little bit of work using an API. The API uses the Unirest http library to simplify the work of receiving from the web. Naturally, since the data is called from the API server, I tried to be efficient by using asynchronous calls to the API. My idea is structured as follows:

  1. 通过返回期货结果创建数据数组
  2. 显示数据 + 从数据中收集的附加信息

因此,我需要将所有数据返回,然后才能开始第二步.我的代码如下:

Therefore, I need to have all the data returned before I can start the second step. My code is as follows:

Future < HttpResponse < JsonNode >  > future1 = Unirest.get("https://example.com/api").asJsonAsync(new Callback < JsonNode > () {
    public void failed(UnirestException e) {
        System.out.println("The request has failed");
    }
    public void completed(HttpResponse < JsonNode > response) {
        System.out.println(response.getBody().toString());
        responses.put(response);
    }
    public void cancelled() {
        System.out.println("The request has been cancelled");
    }
});
Future < HttpResponse < JsonNode >  > future2 = Unirest.get("https://example.com/api").asJsonAsync(new Callback < JsonNode > () {
    public void failed(UnirestException e) {
        System.out.println("The request has failed");
    }
    public void completed(HttpResponse < JsonNode > response) {
        System.out.println(response.getBody().toString());
        responses.put(response);
    }
    public void cancelled() {
        System.out.println("The request has been cancelled");
    }
});
doStuff(responses);

我将如何做到只有在两个期货都完成后才调用 doStuff?

How would I make it so doStuff is called only after both of the futures are finished?

推荐答案

有几个选项.您现在拥有的代码从您发出请求的同一线程调用 doStuff.如果您想阻塞直到两个请求都完成,您可以使用 CountDownLatch.类似的东西:

There are a few options. The code you have now calls doStuff from the same thread where you make your requests. If you want to block until both requests have completed you could use a CountDownLatch. Something like:

CountDownLatch responseWaiter = new CountDownLatch(2);

Future <HttpResponse<JsonNode>> future1 = Unirest.get("https://example.com/api").asJsonAsync(new Callback<JsonNode>() {
  public void completed(HttpResponse<JsonNode> response) {
    responses.put(response);
    responseWaiter.countDown();
  }
  ...
});

// Similar code for the other get call
...

responseWaiter.await();
doStuff(responses);

如果您不想在两个调用完成之前阻塞该线程,您可以让每个匿名内部回调类增加一个 AtomicInteger.当计数为 2 时,您将调用 doStuff.类似的东西:

If you don't want to block that thread until both calls are complete, you could have each of your anonymous inner Callback classes increment an AtomicInteger. When the count is 2 you'd call doStuff. Something like:

AtomicInteger numCompleted = new AtomicInteger();

Future <HttpResponse<JsonNode>> future1 = Unirest.get("https://example.com/api").asJsonAsync(new Callback<JsonNode>() {
  public void completed(HttpResponse<JsonNode> response) {
    responses.put(response);
    int numDone = numCompleted.incrementAndGet();
    if (numDone == 2) {
      doStuff(responses);
    }
  }
});

这篇关于等待多个期货的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆