Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
Last active December 15, 2015 06:49
Show Gist options
  • Save dlhartveld/5219149 to your computer and use it in GitHub Desktop.
Save dlhartveld/5219149 to your computer and use it in GitHub Desktop.
Stateful lambdas lead to non-deterministic stream behavior.
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