publicstaticvoidmain(String[] args) { System.out.println("运行结果:"); List<Integer> numberList = Arrays.asList(1,2,3,4,5,6,7,8,9); // stream method numberList.stream().forEach(number -> { System.out.println(String.format("Stream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number)); }); System.out.println("\r"); // parallelStream method numberList.parallelStream().forEach(number -> { System.out.println(String.format("ParallelStream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number)); }); System.out.println("\r"); // parallelStream method numberList.parallelStream().forEachOrdered(number -> { System.out.println(String.format("ParallelStream forEach Ordered The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number)); }); System.out.println("\r"); }
运行结果: Stream The Current Thread's ID is 1 and output number 1 Stream The Current Thread's ID is 1 and output number 2 Stream The Current Thread's ID is 1 and output number 3 Stream The Current Thread's ID is 1 and output number 4 Stream The Current Thread's ID is 1 and output number 5 Stream The Current Thread's ID is 1 and output number 6 Stream The Current Thread's ID is 1 and output number 7 Stream The Current Thread's ID is 1 and output number 8 Stream The Current Thread's ID is 1 and output number 9
ParallelStream The Current Thread's ID is 1 and output number 6 ParallelStream The Current Thread's ID is 19 and output number 9 ParallelStream The Current Thread's ID is 18 and output number 1 ParallelStream The Current Thread's ID is 15 and output number 2 ParallelStream The Current Thread's ID is 17 and output number 4 ParallelStream The Current Thread's ID is 14 and output number 8 ParallelStream The Current Thread's ID is 13 and output number 3 ParallelStream The Current Thread's ID is 16 and output number 7 ParallelStream The Current Thread's ID is 1 and output number 5
ParallelStream forEach Ordered The Current Thread's ID is 15 and output number 1 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 2 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 3 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 4 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 5 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 6 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 7 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 8 ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 9
Disconnected from the target VM, address: '127.0.0.1:52976', transport: 'socket'