Usage of Stream.forEachUntilCancelled

Brian Goetz brian.goetz at
Sun Mar 24 09:04:54 PDT 2013

It is for processing large or infinite streams under the constraints of 
"find the best answer you can in 5 seconds", "work until you find a 
result at least as good as X", or "work until someone wants to shut the 
system down".  It was designed around the sort of cancellation problems 
outlined in JCiP Ch7, where cancellation is based on global criteria and 
performed on a best-effort basis.

Content-based start/stop criteria are a completely different ball of 
wax, and are more complicated because they are (a) expected to stop 
*immediately* (unlike traditional cooperative cancellation protocols) 
and (b) want to impose encounter order on a parallel problem.  This is 
much more like what limit() currently does than what forEach() does.  We 
are considering such features but not for Java 8.

On 3/24/2013 10:54 AM, Mrityunjay Sharma wrote:
> Hi Brian,
> Then in this case what should be the usage for this method. How a user
> can short circuit the method invocation during the process, as we can
> only pass either true or false (for See Example below).
> * -> System.out.println("Size" +
> s.getSize()), *
> *new BooleanSupplier() { public boolean getAsBoolean(){return false;} });*
> Thanks,
> MJ
> On Sun, Mar 24, 2013 at 7:13 PM, Brian Goetz <brian.goetz at
> <mailto:brian.goetz at>> wrote:
>     You're thinking sequentially.  How would "when does the iteration
>     need to stop" work in parallel?
>     Short answer: forEachUntilCancelled is not what you think it is.
>       There is not a stream primitive for what you want at this time.
>     On 3/24/2013 9:29 AM, Mrityunjay Sharma wrote:
>         Hi All,
>         I was trying to use Stream.forEachUntilCancelled which takes a
>         Consumer and
>         a BooleanSupplier as input parameter, as at any point of time
>         this method
>         can cancel the iteration and return the control. I thought this
>         would work
>         more like while-do statement, so that i can pass the
>         condition(BooleanSupplier) where the iteration needs to stop.
>         But as BooleanSupplier.getAsBoolean does not take any input
>         parameter, then
>         how can we pass a condition where the iteration needs to stop. Is my
>         understanding correct or i am interpreting the method implementation
>         incorrectly.
>         *PS: An implementation Example will be highly appreciated.*
>         Thanks & Regards,
>         MJ

More information about the lambda-dev mailing list