如何使用 CompletableFuture 的 supplyAsync 每次使用多个输入运行相同的方法? [英] How to use supplyAsync of CompletableFuture to run the same method with multiple inputs each time?
问题描述
我在以下代码中创建了一个供应商,并在异步执行后使用 completableFuture 的 supplyAsync 方法调用另一个方法.
I have the following code where I create a supplier and use the completableFuture's supplyAsync method to invoke another method after async execution.
public void runParallelFunctions(MyInput myInput) {
Supplier<Map<String, String>> taskSupplier = () -> {
try {
return invokeLambda("input1");
} catch (Exception e) {
System.out.println(e);
}
return new HashMap<>();
};
for (int i = 0; i < 5; i++) {
CompletableFuture.supplyAsync(taskSupplier::get, executorService)
.thenAccept(this::printResultsFromParallelInvocations);
}
System.out.println("Doing other work....");
}
下面是我执行完成后调用的方法.
Below is the method I call after the execution completes.
private void printResultsFromParallelInvocations(Map<String, String> result) {
result.forEach((key, value) -> System.out.println(key + ": " + value));
}
在上面的代码中,如何调用方法 invokeLambda
传递多个参数,如input1"、input2"?等等.?我可以通过循环生成输入,但是如何使用供应商的某种列表,以便我可以为 supplyAsync 方法调用整个列表?我不能使用 runAsync 方法,因为我有一个返回值,我需要用它来调用 printResultsFromParallelInvocations
.我是期货和异步回调的新手,希望得到任何帮助.提前致谢.
In the above code, how can I call the method invokeLambda
passing multiple arguments like "input1", "input2" etc.? I can generate the inputs through a loop, but how can I use some sort of a list with the supplier so that I can call the entire list for supplyAsync method? I cannot use runAsync method because I have a return value that I need to call printResultsFromParallelInvocations
with. I'm new to futures and async callbacks and would appreciate any help. Thanks in advance.
推荐答案
您不能创建单个 Supplier
并期望它在五个评估中表现不同.它需要外部可变状态才能检测到评估是第 n 次评估,这同时与执行五个没有顺序的并发评估的想法相矛盾.
You can not create a single Supplier<Map<String, String>>
and expect it to behave differently for the five evaluations. It would require external mutable state to make it detectable that an evaluation is the n’th evaluation which at the same time contradicts the idea of performing five concurrent evaluations which have no order.
只需创建五个不同的供应商,例如
Simply create five different suppliers, e.g.
for(int i = 0; i < 5; i++) {
String input = "input" + i;
CompletableFuture.supplyAsync(() -> invokeLambda(input), executorService)
.thenAccept(this::printResultsFromParallelInvocations);
}
在每次循环迭代中,lambda表达式() ->invokeLambda(input)
捕获 input
的当前值并创建一个合适的 Supplier
实例.
In each loop iteration, the lambda expression () -> invokeLambda(input)
captures the current value of input
and creates an appropriate Supplier
instance.
附注:
不要像
invokeLambda
那样以技术方面来命名方法,而是尝试表达它们的目的.
Don’t name methods after technical aspects like
invokeLambda
but rather try to express their purpose.
原始代码中的 taskSupplier::get
是一个不必要的方法引用,因为它产生了一个 Supplier
对已经是 Supplier
的对象调用该方法代码>供应商代码>.因此,如果希望为每个评估获得相同的行为,taskSupplier
可以直接传递给 supplyAsync
.
the taskSupplier::get
in your original code was an unnecessary method reference as it produced a Supplier
invoking the method on an object that was already a Supplier
. So taskSupplier
could have been passed to supplyAsync
directly if getting the same behavior for every evaluation was intended.
这篇关于如何使用 CompletableFuture 的 supplyAsync 每次使用多个输入运行相同的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!