OnJava8-Examples/threads/CountingStream.java

32 lines
925 B
Java
Raw Normal View History

2016-07-05 14:46:09 -06:00
// threads/CountingStream.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;
public class CountingStream {
public static void main(String[] args) {
System.out.println(
IntStream.range(0, 10)
.parallel()
.mapToObj(CountingTask::new)
.map(ct -> ct.call())
.reduce(0, Integer::sum));
}
}
/* Output:
2016-07-27 11:12:11 -06:00
4 ForkJoinPool.commonPool-worker-5 100
2016-07-05 14:46:09 -06:00
2 ForkJoinPool.commonPool-worker-1 100
2016-07-27 11:12:11 -06:00
6 main 100
5 ForkJoinPool.commonPool-worker-1 100
2016-07-22 14:45:35 -06:00
7 ForkJoinPool.commonPool-worker-4 100
2016-07-27 11:12:11 -06:00
1 ForkJoinPool.commonPool-worker-3 100
8 ForkJoinPool.commonPool-worker-2 100
0 ForkJoinPool.commonPool-worker-6 100
9 ForkJoinPool.commonPool-worker-7 100
3 ForkJoinPool.commonPool-worker-5 100
2016-07-05 14:46:09 -06:00
1000
*/