Running on Java 17-ea+15-1230 (Preview)
Home of The JavaSpecialists' Newsletter

288Weakly Consistent Iteration

Author: Dr Heinz M. KabutzDate: 2021-04-01Java Version: 16Category: Language

Abstract: How consistent is weakly-consistent iteration? And which collections return weakly-consistent iterators? How do we know? Join us as we explore what weakly-consistent means with concrete examples.


Welcome to the 288th edition of The Java(tm) Specialists' Newsletter, sent to you from the Island of Crete. I noticed yesterday that I had run for 902 days in a row. Some have a 30 day running streak on their bucket list, but this is that squared. I ran most of my miles with Salomon Speedcross GTX shoes. Winter can get wet here on Crete, and these keep my feet toasty warm while jumping in muddy puddles. Plus the Speedcross give great grip and protection on the rugged mountain paths. I've torn through many a sole since I started running 7 years ago, and almost a dozen bluetooth headsets. And yet, after all that, I am still a big guy. I love food too much :-)

We have three upcoming LIVE virtual classes in April and May 2021:

  1. Refactoring to Streams and Lambdas for US$ 497 on April 6-7 2021 @ 9am-1pm Frankfurt Time.
  2. Extreme Java - Advanced Topics Java 17 Edition for EUR 1299 on April 19-20 2021 @ 9am-5pm Frankfurt Time. (almost sold out)
  3. Design Patterns Deep Dive for US$ 497 on May 11-12 2021 @ 7-11am Los Angeles Time.

My favourite course at the moment is the Refactoring to Streams and Lambdas course. We spend 8 hours ripping apart a 330k LOC application and replacing bits with more modern code. Too much fun! We still have a few places available for next week. The Advanced Topics Course is also very interesting. It is almost sold out though, so please grab the seats if you would like to join.

Weakly Consistent Iteration

In the book 97 Things Every Java Programmer Should Know, I wrote a piece entitled "Read OpenJDK Daily". We can learn a lot from the coding practices in the OpenJDK.

I decided to put this into practice by producing a series of mini-courses to study the java.util.concurrent classes in the minutest of detail. Since I expected that the synchronization in ArrayBlockingQueue would be trivial, I decided to start with that. It also happened to be the first alphabetically, but that is just a coincidence. Indeed, the locking is trivial for the most part. One or two small surprises, but otherwise easy to understand. You can get the ArrayBlockingQueue course here.

What I did not expect is just how complicated the iteration is. The standard ArrayBlockingQueue spans 1638 lines of code (LOC), including comments. Strip out iteration, and we are left with just 944 LOC. Thus over 42% of the class concerns itself with iteration. In my ArrayBlockingQueue Teardown Course, I spend more than an hour explaining the iteration, and I think I rushed that section a bit. For contrast, the LinkedBlockingQueue has 1161 LOC and less than 10% of that is its iterator. ConcurrentLinkedQueue has 1075 LOC and 7.3% of that is the iterator. ArrayDeque has a similar mechanism of circular array to ArrayBlockingQueue, but only 8.5% of its 1235 LOC are needed to create two separate iterators.

And yet, the iteration behaviour of ArrayBlockingQueue is not perfect; it differs slightly from that of its cousins LinkedBlockingQueue et al.

The first question that I want to ask is this: How do we know if an iterator is weakly-consistent? And the second is: What does weakly-consistent even mean? Can we see some examples please?

Which Iterators are Weakly-Consistent?

The iterator() method returns an object that implements the java.util.Iterator interface. It is a classic gang-of-four factory method. The Iterator has methods hasNext(), next(), remove() and forEachRemaining(). However, it does not give us any clues as to what characteristics it might possess. Is it fail-fast like the iterator from ArrayList? Or is it perhaps a snapshot iterator, such as a CopyOnWriteArrayList would return? Or could it be weakly-consistent, such as our dear ArrayBlockingQueue gives us?

Where the Iterator is silent, the Spliterator shouts from the rooftops. We can thus use the spliterator() method to figure out the characteristics. Weakly-consistent spliterators are all CONCURRENT. Here is a demo program that checks the characteristics of the collections from java.util.**:

import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;

import static java.util.Spliterator.*;

public class WhatHasWeaklyConsistentIteration {
  public static void main(String... args) {
    List<Collection<?>> collections = List.of(
        new ArrayBlockingQueue<>(10),
        new ArrayDeque<>(),
        new ArrayList<>(),
        new ConcurrentLinkedDeque<>(),
        new ConcurrentLinkedQueue<>(),
        new ConcurrentSkipListSet<>(),
        new CopyOnWriteArrayList<>(),
        new CopyOnWriteArraySet<>(),
        new DelayQueue<>(),
        new HashSet<>(),
        new LinkedBlockingDeque<>(),
        new LinkedBlockingQueue<>(),
        new LinkedHashSet<>(),
        new LinkedList<>(),
        new LinkedTransferQueue<>(),
        new PriorityBlockingQueue<>(),
        new PriorityQueue<>(),
        new Stack<>(),
        new TreeSet<>(),
        new Vector<>()

    Predicate<Collection<?>> concurrent =
        c -> c.spliterator().hasCharacteristics(CONCURRENT);
    Predicate<Collection<?>> immutable =
        c -> c.spliterator().hasCharacteristics(IMMUTABLE);
    Consumer<Collection<?>> printer =
        c -> System.out.println("\t" + c.getClass().getName());

    System.out.println("Weakly-Consistent (CONCURRENT)");
    System.out.println("Snapshot (IMMUTABLE)");

The output for Java 16 is (this might change in future versions of Java):

Weakly-Consistent (CONCURRENT)
Snapshot (IMMUTABLE)

The first list is that of the weakly-consistent spliterators. We can apply the same characteristics also to the iterators. Fortunately there are only 8 such collections that we need to investigate. Of the BlockingQueue variety we have ArrayBlockingQueue, LinkedBlockingDeque/Queue and LinkedTransferQueue. Then we have four concurrent collections ConcurrentLinkedDeque/Queue, ConcurrentSkipListSet and a set constructed from a ConcurrentHashMap. They are all marked as having CONCURRENT spliterators, and thus we can presume they are also weakly-consistent.

Next we have IMMUTABLE iterators, the most important being the CopyOnWrite collections, which make a copy of the backing array whenever we change it. The iterator would hold the old reference to the array, so that the iteration would always be over the collection as it was when we created the iterator. We call these snapshot iterators. They are fast to iterate, but slow to change. The CopyOnWrite collections have proven useful, but I have never used singletonList/Set(). IMMUTABLE says that the structure of the collection should not change, though the contents may. Arrays.asList() could be IMMUTABLE, but it is not a snapshot iterator.

The list under "Neither" is a mixed bag. List.of(...), Set.of(...) and Collections.emptyList/Set() cannot throw a ConcurrentModificationException. Neither can Arrays.asList(). The rest are fail-fast iterators and absolutely can and will throw ConcurrentModificationExceptions if we look at them funny.

Examples of Weakly-Consistent Behaviour

If nothing changes in a collection after we have made an iterator, we would expect to see all the elements in the collection, albeit not necessarily in the same order as we added them. However, if the collection changes after we have created an iterator, we might or might not see those changes.

To try this out, I wrote a series of tests that experiment with adding before or after the current iterator position, removing before, on, or after the current position, clearing the collection entirely, and a couple of other weird behaviours.

The simplest way of understanding weakly-consistent is that we will never get a ConcurrentModificationException, whilst at the same time never seeing the same element twice (unless it is there twice) and that we also won't skip over elements that were always in the collection.

Vector's Enumeration was neither fail-fast nor weakly-consistent. We could create an Enumeration, but even if hasMoreElements() was true, nextElement() could throw a NoSuchElementException. Also, with Vector if we removed an element before our current position, then we would skip over the next element. Similarly, adding an element before our current position would make us see duplicate elements. These effects do not happen with weakly-consistent iterators.

I wrote a test class that you can find here: Since it is quite long, I will only show snippets in this newsletter.

For our first example, we want to test what happens if we create an iterator prior to adding one element:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

The behaviour is consistent. None of the concurrent iterators print anything.

What if we change it to start with a single element, like so:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

ArrayBlockingQueue prints only the 0, all the other concurrent iterators print 0, 1. Why? Good that you ask, and I happen to have a comprehensive ArrayBlockingQueue course for that ;-) Note though that the ArrayBlockingQueue is allowed to omit 1. It would not be allowed to omit 0, since that was always there, from when the iterator was created.

What if the collection starts with two or more elements, then we create the iterator and add one more element?

test(collection -> {
  Iterator<Integer> it = collection.iterator();

This time the behaviour is consistent again and we see 0, 1, 2 for all iterators. However, in Java 7, the ArrayBlockingQueue only showed 0, 1. That was legal behaviour, but not desirable. It explains why they reworked the iteration of ArrayBlockingQueue for Java 8. In Java 7 this class was only 924 LOC with iteration taking up a mere 9.2%.

The next is a bit more tricky. I want to add an element before our current iteration position. If the collection is not ORDERED, we can assume it is a hash map. If we know a bit about the inner workings, we can make a wild guess that a smaller Integer would be before the bigger Integer, at least for small ranges of Integer. This was not the case prior to Java 8. Similarly, if it is a SORTED collection, then we can again add a smaller number. Lastly, if it is a Deque, we can use addFirst(). Note that I'm using the Java 16 instanceof pattern matching. It's a feature that I would love to never need, but it has proven useful once or twice. For the other collections we simply skip this test and move on.

test(collection -> {
  Iterator<Integer> it = collection.iterator();
  if (!ordered(collection)) {
    collection.add(0); // happens to put it before the 5
  } else if (sorted(collection)) {
  } else if (collection instanceof Deque<Integer> deque) {
  } else {
    System.out.println("\t\tskip ...");

All the collections where we were able to insert in front showed 5, 6. Vector under similar conditions would print 5, 5, 6, thus showing one element twice.

Next we want to try removing elements, starting with calling clear() after we have created our iterator:

Iterator<Integer> it = collection.iterator();

A bit unexpectedly, all the concurrent iterators print 0. The thinking is, if we have created an iterator, we might also have called hasNext() by the time we call next(). Thus if there are any elements in the collection when we create the iterator, it keeps a reference to that first element in case it needs it. Vector was more gung-ho and simply threw NoSuchElementException.

In our next test we remove the first element of three:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

Taken what we have learned so far, we could assume that the answer would be 0, 1, 2. And we would be correct :-)

Next we remove an element after our current position:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

This also works as expected and prints 0, 2.

The next test is a bit more tricky. We start with three elements. We then create an iterator and advance it to the second element. Next we remove the first element and see what is printed out:

test(collection -> {
  Iterator<Integer> it = collection.iterator();; // 0

They all show 1, 2. A Vector/Enumeration would omit 1 and only print 2.

Next we add 5 elements, create the iterator and then remove the first two:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

All of the iterators consistently print 0, 2, 3, 4. Vector would have printed 2, 3, 4, which is a bit more correct in this case, but the other faults make it unusable.

So far, so good. Now it gets peculiar. We fill the collection with 5 elements. We then create the iterator and remove and add another 5 elements.

test(collection -> {
  for (int i = 0; i < LENGTH; i++) collection.add(i);
  Iterator<Integer> it = collection.iterator();
  for (int i = 0; i < LENGTH * 2; i++) {
    collection.add(i + LENGTH);

Guess what happens? Well, ArrayBlockingQueue shows 0, all others show 0, 10, 11, 12, 13, 14. Both behaviours are legal for weakly-consistent iterators. In Java 7 the ArrayBlockingQueue would have shown 10, 11, 12, 13, 14.

The last experiment tries to remove the last element by calling it.remove() after we are done iterating:

test(collection -> {
  Iterator<Integer> it = collection.iterator();

The ArrayBlockingQueue throws an IllegalStateException. This is deliberate. In the class we read this comment:

// Calling forEachRemaining is a strong hint that this
// iteration is surely over; supporting remove() after
// forEachRemaining() is more trouble than it's worth

I can imagine the JSR166 team late on a Friday night trying to finish this code and wondering whether anyone on this planet would ever want to call remove after forEachRemaining(). Doug Lea turns to Martin Buchholz and shakes his head. Nope, no one. Well, there is this fellow on the Island of Crete ... he might. But let's hope he does not notice.

Kind regards




We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.