stream.parallel().limit() not usable
Brian Goetz
brian.goetz at oracle.com
Sat Oct 5 09:12:33 PDT 2013
> For completeness I want to show how I could rewrite the code using a
> streams-based implementation:
>
> final AtomicInteger elementsConsumed = new AtomicInteger(0);
> IntStream.range(0, maxToGenerate)
> .parallel()
> .mapToObj(i -> generatorFunction.get())
> .filter(condition::test)
> .peek(action::accept)
> .mapToInt(element ->
> elementsConsumed.incrementAndGet())
> .filter(n -> n >= needed)
> .findFirst();
If this code works for you, then what you're saying is that you don't care about order. Which I believe. In which case, just use .unordered() in your stream and you'll get good parallelism without having to contort your code. Try it and report back?
You might also do better with Stream.generate, since it creates an unordered stream:
Stream.generate(generatorFunction)
.parallel()
...
More information about the lambda-dev
mailing list