Page 1 of 1

About Question enthuware.ocpjp.v8.2.1708 :

Posted: Tue Jan 15, 2019 2:48 pm
by AungKNyar
The answer is: Both the threads will complete their operations successfully without getting any exception.
Is it because of CopyOnWriteArrayList<>()?
And two threads are operating on different lists? Copied one and the original one?

There are no explanations provided so I posted here.

Re: About Question enthuware.ocpjp.v8.2.1708 :

Posted: Tue Jan 15, 2019 9:44 pm
by admin
Yes, when you acquire an Iterator from CopyOnWriteArrayList, a copy of the underlying array of the data is created. The iterator uses this copy instead of the original one. That is why the add operation can work on the list at the same time another thread is iterating through the iterator.

An implication of this is that any modifications done to the list are not reflected in the Iterator and no modifications can be done on the list using that Iterator (such as by calling iterator.remove() ). Calls that try to modify the iterator will get UnsupportedOperationException.