John Mulholland wrote:Hi guys, wondering if anyone could shed some light on this. ArrayDeque's iterator is fail-fast. Here is a simple example of the collection being amended after an iterator is created. This code throws a ConcurrentModificationException when I try to use the iterator after I call add(E e) OR addLast(E e) but NOT when I call addFirst(E e). Any idea why this is ? Is addFirst(E e) not considered to be a structural alteration to the collection ?
UPDATE : In fact none of the methods addFirst, offerFirst, removeFirst, pollFirst result in a subsequent throw of ConcurrentModificationException when the iterator's next() method is called (compared with a call to equivalent somethingLast methods which do result in a throw of ConcurrentModificationException when the iterator's next() method is called) ?
If you are asking about the logic, I would say it's just the expected behavior of 'DeqIterator'. You can see when constructing the iterator, it will pass the current ArrayDeque's 'tail' to the attribute '
fence' of 'DeqIterator', but everytime you call 'addLast', it will increase the tail by one, and the exception will be thrown if 'tail != fence'.