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

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

问题描述

最近,我深入探讨了使用API​​的工作一点点。该API使用HTTP Unirest库来简化从网络接收的工作。自然地,由于数据是从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. 显示数据+从收集的数据的附加信息

所以,我需要回来之前,我就可以开始第二个步骤中的所有数据。我的code是如下:

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?

推荐答案

有几个选项。在code你现在叫 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天全站免登陆