[英]Is there a performance difference between == and > or < operations in java specifically
[英]Is there performance advantage to aggregating async operations before proceeding to sync operations in Java?
我有一个 Spring 引导 MVC 应用程序,其中我有一个 Java 方法:
对于 HTTP 请求,我将使用异步客户端,例如 Apache HTTP 异步客户端。 我想知道首先在一个循环中执行所有 HTTP 调用(异步操作)然后有另一个循环来执行业务逻辑以及更新数据库是否有任何性能优势,或者只需一个结合同步的循环就可以了每次迭代的 /async 操作。 所有操作都将使用Future
/ CompletableFuture
:
2个循环版本:
public String testWithTwoLoops() throws Exception {
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom()
.setMaxConnPerRoute(1000)
.setMaxConnTotal(1000)
.build();
httpAsyncClient.start();
List<Future<HttpResponse>> futureList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
HttpGet httpGet = new HttpGet("http://localhost:3001");
Future<HttpResponse> responseFuture = httpAsyncClient.execute(httpGet, null);
futureList.add(responseFuture);
}
for (int i = 0; i < 100; i++) {
futureList.forEach(f -> {
try {
HttpResponse httpResponse = f.get();
// perform business logic/update db
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
}
return "result";
}
1个循环版本:
public String testWithOneLoop() throws Exception {
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom()
.setMaxConnPerRoute(1000)
.setMaxConnTotal(1000)
.build();
httpAsyncClient.start();
List<CompletableFuture<HttpResponse>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
CompletableFuture<HttpResponse> httpResponseCompletableFuture = apiCallWithBusinessLogicAndDbUpdate(httpAsyncClient);
futures.add(httpResponseCompletableFuture);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
.join();
return "result";
}
简短的回答 - 首先触发所有异步 HTTP 请求会更好为什么 - HTTP 调用需要更长的时间来完成(因此,当您执行所有业务逻辑和数据库更新时,(所有)Z293C9EA246FF9985DC6F62A650 调用正在完成)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.