4

For instance, let's say there is a some Collections#reverse(List) operation that uses the ListIterator as such:

var forwardItr = list.listIterator();
var reverseItr = list.listIterator(list.size());
while (forwardItr.nextIndex() < reverseItr.previousIndex()) {
  var forward = forwardItr.next();
  var reverse = reverseItr.previous();
  forwardItr.set(reverse)
  reverseItr.set(forward)
}

Should there ever be some implementation that throws a ConcurrentModificationException from ListIterator#set? Or rather, is there a particular type of modification (i.e. "structural") that should cause an exception throw? Is it implied that some implementations of List may justifiably throw an exception from the aforementioned operation?

bizness86
  • 57
  • 7

1 Answers1

5

Should there ever be some implementation that throws a ConcurrentModificationException from ListIterator::set?

The answer is that there could be.

The javadocs for List, ListIterator and ConcurrentModificationException talk it terms of modifications that are permissible and not permissible without going into the specifics of what is permissible. If you look at the javadocs for (say) ArrayList you will see that it says that changes that don't cause structural modifications are permissible during an iteration. However, that doesn't apply to all list types; e.g. all modifications are permitted during an iteration for a CopyOnWriteArrayList.

A custom list type could place different constraints on modifications.

Or rather, is there a particular type of modification (i.e. "structural") that should cause an exception throw?

Well ListIterator::set is not a structural modification. But for some list classes a "structural" modification during iteration will lead to a CME.

Other (hypothetical) examples:

  • A custom list class could be implemented that did not permit set operations if (say) two iterators were active, and throw a CME if this happened.

  • In a custom list that was a sorted view of something else, a set call that broke the ordering could throw a CME.

Arguable these could be a different exceptions; e.g. UnsupportedOperationException. My reading of the javadocs are that CME would be appropriate.

Is it implied that some implementations of List may justifiably throw an exception from the aforementioned operation?

Yes. A custom List implementation could do all sorts of "interesting" things so long as it conforms to the behaviors defined in the List and Collection APIs.


Q: Do you need to allow for this in your code?

A: IMO is reasonable to write your code so that it works for "normal" lists. It is not possible to allow for all of the crazy things that a custom list class might do.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I appreciate your speedy response. I'd typically want to use best practices to the best of my ability, and checking for concurrent modification seems generally encouraged; though whether or not to check seems to require greater-than-basic judgment. – bizness86 Mar 31 '19 at 04:07
  • Checking for CME is probably a bad idea. If it occurs, it indicates that there is a fault in the program as a while. You can't recover from it in this code. Therefore testing for it in this code is the wrong thing to do. (Allowing an *unexpected* unchecked exception to propagate is *good* practice ...) – Stephen C Mar 31 '19 at 04:10
  • Forgive me; I meant to convey what you have stated in parenthesis: (Allowing an unexpected unchecked exception to propagate is good practice ...) – bizness86 Mar 31 '19 at 04:14
  • Also ... can I encourage you to read this: http://www.satisfice.com/blog/archives/27. The take-away should be that looking for "best practice" or "good practice" dogma that you can follow without thinking is misguided. You need to understand the issues, and then work out *for your self* what is appropriate in each specific context. (And sometimes, the most appropriate thing can be to ignore *good practice* advice.) – Stephen C Mar 31 '19 at 04:16
  • OK, I mis-understood you; but ... *"and checking for concurrent modification seems generally encouraged"* - I have **do not recall** ever seeing someone say that application code should check for CMEs. And I have **do not recall** ever seeing anyone "encouraging" custom list implementers to do this. Do you have examples? – Stephen C Mar 31 '19 at 04:20