我在Spring boot应用程序中有一个用例,如下所示:
我想使用以下函数从响应中获取id字段值:
String id = getIdFromResponse(response);如果我在响应中没有获得任何id,那么我将使用以下函数检查请求参数中是否存在id字段:
String id = getIdFromRequest(request);到目前为止,我将按顺序调用它们。但是我想让这两个函数并行运行,一旦我从它们中的任何一个得到id,我就会停止。
我想知道是否有任何方法可以在Java8中使用streams来实现这一点。
发布于 2020-10-21 15:02:04
您可以使用类似以下内容:
String id = Stream.<Supplier<String>>of(
() -> getIdFromResponse(response),
() -> getIdFromRequest(request)
)
.parallel()
.map(Supplier::get)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow():供应商是必需的,因为当您不使用它们时,两个请求仍然是按顺序执行的。
我还假设您的方法在未找到任何内容时返回null,因此我必须使用.filter(Objects::nonNull)过滤掉这些值。
根据您的用例,您可以用不同的东西替换.orElseThrow(),比如.orElse(null)
发布于 2020-10-21 15:40:48
没有必要使用Stream API,只要有一个方法就可以做到这一点。
ExecutorService::invokeAny(Collection>)
执行给定的任务,返回已成功完成的任务的结果(即,没有抛出异常)。在正常或异常返回时,取消未完成的任务。
List<Callable<String>> collection = Arrays.asList(
() -> getIdFromResponse(response),
() -> getIdFromRequest(request)
);
// you want the same number of threads as the size of the collection
ExecutorService executorService = Executors.newFixedThreadPool(collection.size());
String id = executorService.invokeAny(collection);三个注意事项:
如果没有及时可用的结果,还有一个超时抛出TimeoutException的重载方法:invokeAny(Collection>, long, TimeUnit)
ExecutionException和InterruptedException完成后忘记关闭invokeAny 发布于 2020-10-21 21:45:57
如果您希望完全控制何时启用备用评估,则可以使用CompletableFuture
CompletableFuture<String> job
= CompletableFuture.supplyAsync(() -> getIdFromResponse(response));
String id;
try {
id = job.get(300, TimeUnit.MILLISECONDS);
}
catch(TimeoutException ex) {
// did not respond within the specified time, set up alternative
id = job.applyToEither(
CompletableFuture.supplyAsync(() -> getIdFromRequest(request)), s -> s).join();
}
catch(InterruptedException|ExecutionException ex) {
// handle error
}仅当第一个作业未在指定时间内完成时,才会提交第二个作业。然后,无论哪个作业最先响应,都将提供结果值。
https://stackoverflow.com/questions/64458149
复制相似问题