Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created August 17, 2015 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/7cb5e139ebc093d9cc45 to your computer and use it in GitHub Desktop.
Save Dierk/7cb5e139ebc093d9cc45 to your computer and use it in GitHub Desktop.
an impure parallel stream that should only map but actually serves as random number generator
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