英文:
How to perform the transformation on CompletableFuture tasks
问题
我正在尝试对CompletableFuture任务列表进行一些转换,但由于缺乏经验而遇到了一些问题。我有一个CompletableFuture任务的列表,但我未能对其执行进一步的转换。
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class thenapply {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Integer> list = Arrays.asList(5, 9, 14);
final int sum = 0;
List<CompletableFuture<Integer>> ans = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
final int finali = i;
ans.add(CompletableFuture.supplyAsync(() -> square(list.get(finali))));
}
System.out.println(ans.stream()
.mapToInt(Integer::intValue).sum());
}
private static int square(int a) {
return a * a;
}
}
我知道`.mapToInt(Integer::intValue)`是错误的,但不知道如何纠正它。此外,我在阅读中了解到我们应该使用`allOf`,但我不确定如何在这里实现它。
请帮忙。
英文:
I am trying to perform some transformation on the list of CompletableFuture tasks, but facing some issue because of lack of experience. I have a list of CompletableFuture but I am failing to perform further transformation on it.
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class thenapply {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Integer> list = Arrays.asList(5, 9, 14);
final int sum = 0;
List<CompletableFuture<Integer>> ans = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
final int finali = i;
ans.add(CompletableFuture.supplyAsync(() -> square(list.get(finali))));
}
System.out.println(ans.stream()
.mapToInt(Integer::intValue).sum());
}
private static int square(int a) {
return a * a;
}
}
I know .mapToInt(Integer::intValue)
is wrong but do not know how to correct it. Further, I am reading that we should use allOf
but I am not sure how to implement it here.
Please help.
答案1
得分: 0
你需要等待完成后才能进行进一步的计算。
因此,不要使用你最后的 System.out.println
,而是要进行如下操作:
// 等待并处理各个值
int result = 0;
for (var f : ans) {
result += f.get();
}
System.out.println(result);
英文:
You need to wait for completion before you can do further calculation.
So, instead of your last System.out.println
, do something along
// wait and process individual values
int result = 0;
for (var f : ans) {
result += f.get();
}
System.out.println(result);
答案2
得分: 0
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Integer> list = Arrays.asList(5, 9, 14);
final AtomicInteger sum = new AtomicInteger(0);
int size = list.size();
CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
final int finali = i;
CompletableFuture.runAsync(() -> {
int sq = square(list.get(finali));
sum.addAndGet(sq);
latch.countDown();
});
}
latch.await();
System.out.println(sum.get());
}
英文:
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Integer> list = Arrays.asList(5, 9, 14);
final AtomicInteger sum = new AtomicInteger(0);
int size = list.size();
CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
final int finali = i;
CompletableFuture.runAsync(() -> {
int sq= square(list.get(finali));
sum.addAndGet(sq);
latch.countDown();
});
}
latch.await();
System.out.println(sum.get());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论