an impure parallel stream that should only map but actually serves as random number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.function.*; | |
import java.util.stream.*; | |
public class ParallelStreamTest { | |
static class IntGenerator implements Supplier<Integer> { | |
private int current = 0; | |
public Integer get() { | |
return current++; | |
} | |
} | |
public static final void main(String[] args) { | |
Stream<Integer> s = Stream.generate(new IntGenerator()); | |
List<Integer> xs = s | |
.limit(10) | |
.parallel() | |
.map(x -> x * 2) | |
.collect(Collectors.toList()); | |
System.out.println(xs); | |
} | |
} | |
// Kudos: Volker Steiss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment