Stream allMatch issue

Brian Goetz brian.goetz at oracle.com
Tue Nov 27 09:47:43 PST 2012


I think it is reasonable for users to expect that when the bulk 
operation returns, we are finished with the stream and its lambdas.  So, 
I would count this as an error.

On 11/27/2012 12:25 PM, Georgiy Rakov wrote:
> Hello,
>
> the code below causes ConcurrentModificationException to be thrown with
> following stack trace:
>
>     Exception in thread "main" java.util.ConcurrentModificationException
>          at
>     java.util.ArrayList$Itr.checkForComodification(ArrayList.java:818)
>          at java.util.ArrayList$Itr.next(ArrayList.java:790)
>          at AllMatchBehaviour.main(AllMatchBehaviour.java:34)
>
> Obviously the emphasized piece of code is one part of the cause (see
> below).
>
>     import java.util.ArrayList;
>     import java.util.Arrays;
>     import java.util.List;
>     import java.util.functions.Predicate;
>
>     public class AllMatchBehavior {
>          public static void main(String[] args) {
>              ArrayList<Integer> calledObjects = new ArrayList<>();
>              List<Integer> streamContent = new ArrayList<>();
>              for (int i = 0; i < 10000; ++i) {
>                  streamContent.add(i);
>              }
>
>              Predicate<Integer> p = i -> {
>                  synchronized (calledObjects) {
>                      calledObjects.add(i);
>                  }
>                  return i < 5000;
>              };
>
>              boolean result = Arrays.parallel(streamContent.toArray(new
>     Integer[]{})).allMatch(p);
>
>              String asStr = "";
>     *        for (Integer i : calledObjects) {*
>     *            asStr += " " + i;*
>     *        }*
>              System.out.println("called objects: " + asStr);
>          }
>     }
>
> I could guess that exception is thrown because some stream chunks are
> still processed after the result have been received from allMatch.
> Is such behavior considered to be faulty? Please give your comments.
>
> The same things happen to noneMatch and anyMatch methods by the way.
>
> The source code is attached for your convenience.
>
> Thanks, Georgiy.
>
>
>


More information about the lambda-dev mailing list