OnJava8-Examples/concurrent/ParallelStreamPuzzle.java

29 lines
766 B
Java
Raw Normal View History

2016-11-23 09:05:26 -08:00
// concurrent/ParallelStreamPuzzle.java
// (c)2021 MindView LLC: see Copyright.txt
2016-07-05 14:46:09 -06:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-05 14:46:09 -06:00
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class ParallelStreamPuzzle {
static class IntGenerator
implements Supplier<Integer> {
private int current = 0;
@Override public Integer get() {
2016-07-05 14:46:09 -06:00
return current++;
}
}
public static void main(String[] args) {
List<Integer> x =
Stream.generate(new IntGenerator())
.limit(10)
.parallel() // [1]
2016-07-05 14:46:09 -06:00
.collect(Collectors.toList());
System.out.println(x);
}
}
/* Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*/