"is list.iterator() thread-safe?" Code Answer

5

the behaviour of list.iterator() is not defined or consistent with different list implementations.

for arraylist, linkedlist, you can get a concurrentmodificationexception if the list is modified when you are iterating over it. (this is not guaranteed) the way to avoid this issue is to use a synchronizedlist() and lock the list while iterating over it.

for vector, the collection is synchronized, but the iterator is not thread safe.

for copyonwritearraylist, you get a snapshot of the elements in the list at the time you call iterator(), this iterator is thread safe, and you don't need to use any locking. note: the contents of the elements can change.

By CVS on January 13 2022

Answers related to “is list.iterator() thread-safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.