Is List.iterator() thread-safe?

后端 未结 3 764
误落风尘
误落风尘 2020-12-05 14:14

In Java: Is List.iterator() thread-safe, i.e. does the returned iterator reflect the current state of the list at any time or just the state of the list at the

3条回答
  •  -上瘾入骨i
    2020-12-05 14:49

    No iterator is thread-safe. If the underlying collection is changed amidst iteration, a ConcurrentModificationException is thrown.

    Even iterators of synchronized collections are not thread-safe - you have to synchronize manually.

    One exception is the CopyOnWriteArrayList, which holds a snapshot during iteration.

提交回复
热议问题