Joh*_*ace 9 java api concurrency future
最近我使用API深入研究了一些工作.API使用Unirest http库来简化从Web接收的工作.当然,由于从API服务器调用数据,我试图通过使用API的异步调用来提高效率.我的想法结构如下:
因此,在开始第二步之前,我需要返回所有数据.我的代码如下:
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);
Run Code Online (Sandbox Code Playgroud)
我怎样才能这样做,只有在两个期货完成后才会调用doStuff?
有几个选择.您现在使用的代码doStuff
来自您发出请求的同一个线程.如果要阻止两个请求都完成,则可以使用CountDownLatch.就像是:
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);
Run Code Online (Sandbox Code Playgroud)
如果您不希望在两个调用完成之前阻塞该线程,则可以让每个匿名内部Callback类递增AtomicInteger.当计数为2时,你会打电话doStuff
.就像是:
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);
}
}
});
Run Code Online (Sandbox Code Playgroud)