对于下面的程序,我试图弄清楚为什么使用2个不同的流并行化任务,并使用相同的流并在Completable future上调用join / get使它们花费更长的时间,就好像它们被顺序处理一样。] >
public class HelloConcurrency { private static Integer sleepTask(int number) { System.out.println(String.format("Task with sleep time %d", number)); try { TimeUnit.SECONDS.sleep(number); } catch (InterruptedException e) { e.printStackTrace(); return -1; } return number; } public static void main(String[] args) { List<Integer> sleepTimes = Arrays.asList(1,2,3,4,5,6); System.out.println("WITH SEPARATE STREAMS FOR FUTURE AND JOIN"); ExecutorService executorService = Executors.newFixedThreadPool(6); long start = System.currentTimeMillis(); List<CompletableFuture<Integer>> futures = sleepTimes.stream() .map(sleepTime -> CompletableFuture.supplyAsync(() -> sleepTask(sleepTime), executorService) .exceptionally(ex -> { ex.printStackTrace(); return -1; })) .collect(Collectors.toList()); executorService.shutdown(); List<Integer> result = futures.stream() .map(CompletableFuture::join) .collect(Collectors.toList()); long finish = System.currentTimeMillis(); long timeElapsed = (finish - start)/1000; System.out.println(String.format("done in %d seconds.", timeElapsed)); System.out.println(result); System.out.println("WITH SAME STREAM FOR FUTURE AND JOIN"); ExecutorService executorService2 = Executors.newFixedThreadPool(6); start = System.currentTimeMillis(); List<Integer> results = sleepTimes.stream() .map(sleepTime -> CompletableFuture.supplyAsync(() -> sleepTask(sleepTime), executorService2) .exceptionally(ex -> { ex.printStackTrace(); return -1; })) .map(CompletableFuture::join) .collect(Collectors.toList()); executorService2.shutdown(); finish = System.currentTimeMillis(); timeElapsed = (finish - start)/1000; System.out.println(String.format("done in %d seconds.", timeElapsed)); System.out.println(results); } }
输出
WITH SEPARATE STREAMS FOR FUTURE AND JOIN
Task with sleep time 6
Task with sleep time 5
Task with sleep time 1
Task with sleep time 3
Task with sleep time 2
Task with sleep time 4
done in 6 seconds.
[1, 2, 3, 4, 5, 6]
WITH SAME STREAM FOR FUTURE AND JOIN
Task with sleep time 1
Task with sleep time 2
Task with sleep time 3
Task with sleep time 4
Task with sleep time 5
Task with sleep time 6
done in 21 seconds.
[1, 2, 3, 4, 5, 6]
对于下面的程序,我试图弄清楚为什么使用2个不同的流并行处理任务,并使用相同的流并在Completable future上调用join / get使它们花费更长的时间...
map
操作的顺序,因为它不打算用于可能是相关问题的用例。因此,您的第二个版本执行的特定方式实质上等效于