iservice list方法_java-将List <Future>转换为Future <List>序列

免责声明:这不会完全回答最初的问题。 它将缺少“如果一个失败就全部失败”部分。 但是,我无法回答实际的,更笼统的问题,因为它是作为以下内容的副本而被关闭的:Java 8 CompletableFuture.allOf(...),带有Collection或List。 所以我会在这里回答:

如何转换List>为   CompletableFuture>使用Java 8的流API?

摘要:使用以下内容:

private CompletableFuture> sequence(List> listOfFutures) {

CompletableFuture> identity = CompletableFuture.completedFuture(new ArrayList<>());

BiFunction>, CompletableFuture, CompletableFuture>> accumulator = (futureList, futureValue) ->

futureValue.thenCombine(futureList, (value, list) -> {

List newList = new ArrayList<>(list.size() + 1);

newList.addAll(list);

newList.add(value);

return newList;

});

BinaryOperator>> combiner = (futureList1, futureList2) -> futureList1.thenCombine(futureList2, (list1, list2) -> {

List newList = new ArrayList<>(list1.size() + list2.size());

newList.addAll(list1);

newList.addAll(list2);

return newList;

});

return listOfFutures.stream().reduce(identity, accumulator, combiner);

}

用法示例:

List> listOfFutures = IntStream.range(0, numThreads)

.mapToObj(i -> loadData(i, executor)).collect(toList());

CompletableFuture> futureList = sequence(listOfFutures);

完整的例子:

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.CompletableFuture;

import java.util.concurrent.Executor;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.ThreadLocalRandom;

import java.util.function.BiFunction;

import java.util.function.BinaryOperator;

import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

public class ListOfFuturesToFutureOfList {

public static void main(String[] args) {

ListOfFuturesToFutureOfList test = new ListOfFuturesToFutureOfList();

test.load(10);

}

public void load(int numThreads) {

final ExecutorService executor = Executors.newFixedThreadPool(numThreads);

List> listOfFutures = IntStream.range(0, numThreads)

.mapToObj(i -> loadData(i, executor)).collect(toList());

CompletableFuture> futureList = sequence(listOfFutures);

System.out.println("Future complete before blocking? " + futureList.isDone());

// this will block until all futures are completed

List data = futureList.join();

System.out.println("Loaded data: " + data);

System.out.println("Future complete after blocking? " + futureList.isDone());

executor.shutdown();

}

public CompletableFuture loadData(int dataPoint, Executor executor) {

return CompletableFuture.supplyAsync(() -> {

ThreadLocalRandom rnd = ThreadLocalRandom.current();

System.out.println("Starting to load test data " + dataPoint);

try {

Thread.sleep(500 + rnd.nextInt(1500));

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("Successfully loaded test data " + dataPoint);

return "data " + dataPoint;

}, executor);

}

private CompletableFuture> sequence(List> listOfFutures) {

CompletableFuture> identity = CompletableFuture.completedFuture(new ArrayList<>());

BiFunction>, CompletableFuture, CompletableFuture>> accumulator = (futureList, futureValue) ->

futureValue.thenCombine(futureList, (value, list) -> {

List newList = new ArrayList<>(list.size() + 1);

newList.addAll(list);

newList.add(value);

return newList;

});

BinaryOperator>> combiner = (futureList1, futureList2) -> futureList1.thenCombine(futureList2, (list1, list2) -> {

List newList = new ArrayList<>(list1.size() + list2.size());

newList.addAll(list1);

newList.addAll(list2);

return newList;

});

return listOfFutures.stream().reduce(identity, accumulator, combiner);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值