如何在CompletableFuture任务上执行转换:

huangapple go评论43阅读模式
英文:

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&lt;Integer&gt; list = Arrays.asList(5, 9, 14);

        final int sum = 0;

        List&lt;CompletableFuture&lt;Integer&gt;&gt; ans = new ArrayList&lt;&gt;();

        for (int i = 0; i &lt; list.size(); i++) {

            final int finali = i;

            ans.add(CompletableFuture.supplyAsync(() -&gt; 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&lt;Integer&gt; 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 &lt; size; i++) {
        final int finali = i;
        CompletableFuture.runAsync(() -&gt; {
            int sq= square(list.get(finali));
            sum.addAndGet(sq);
            latch.countDown();
        });
    }
    latch.await();
    System.out.println(sum.get());
}

huangapple
  • 本文由 发表于 2020年9月9日 13:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定