Last active
December 15, 2015 06:49
-
-
Save dlhartveld/5219149 to your computer and use it in GitHub Desktop.
Stateful lambdas lead to non-deterministic stream behavior.
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
List<String> ss = ...; | |
List<String> result = ...; | |
Stream<String> stream = ss.stream(); | |
stream.map(s -> { | |
synchronized (result) { | |
if (result.size() < 10) { | |
result.add(s); | |
} | |
} | |
}) | |
.forEach(e -> { }); | |
// The contents of result will depend on the order of execution. | |
// If this stream is run in parallel, result.add(s) is called | |
// for some elements, depending on which thread/spliterator is | |
// called first. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment