package org.jimmy.test2019091701; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.Executors; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author YueShen * @date 2019年9月17日 下午5:48:33 * @detail */ public class Test { public static Executor executor = Executors.newFixedThreadPool(30); public static <T>List<T> forkJoin(List<Callable<T>> list){ CompletionService<T> service = new ExecutorCompletionService<T>(executor); list.forEach(service::submit); return Stream.generate(() -> 1).limit(list.size()).map(x -> { try{ return service.take().get(); }catch(Exception e){ } return null; }).collect(Collectors.toList()); } public static <T>List<T> forkJoin(Callable<T>... callables){ return forkJoin(Arrays.asList(callables)); } public static <T>Set<T> test(){ return null; } public static void main(String[] args) { List<Integer> numList = forkJoin(() -> -2, () -> -1, () -> 0, () -> -1 ); System.out.println(numList); numList = forkJoin(() -> 1 + 1, () -> 2 + 2); System.out.println(numList); } }