0

Oracle says

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

Could this mean that even if multiple threads are iterating together over the same collection's fail-fast implementation's (Vector,Hashmap,ArrayList,HashSet) object carry out iterator.remove() there would be no ConcurrentModificationException thrown?

halfer
  • 19,824
  • 17
  • 99
  • 186
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
  • No it does not. With the exception of Vector it will likely happen with multiple threads. – Adam Gent Jul 05 '16 at 10:51
  • 1
    No it does Not.If multiple threads are iterating over the same collection's fail-fast implementation's there would be `ConcurrentModificationException`, It is not generally permissible for one thread to modify a Collection while another thread is iterating over it. – Mihir Jul 05 '16 at 10:56
  • `@Mihir` Just a little bit clarity. What if the "modification" is **iterator.remove()** by one thread when it is traversing and the other thread executes the same while iterating *during the iteration of the former thread*? – Aakash Verma Jul 06 '16 at 05:15

2 Answers2

2

No. This tells you that only safe way to remove elements while iterating (in one thread) is to use iterator.remove. And if collection is accessed (iterated or modified) from other threads - sometime you will get exception, sometime not - in general behavior is not deterministic so you should avoid using it or relying on it.

That being said - only exception to this are Concurrent collections.

halfer
  • 19,824
  • 17
  • 99
  • 186
river
  • 774
  • 5
  • 16
  • So you mean concurrent collections do not face the problem of having multiple threads executing **iterator.remove()** all at once (or for that matter anytime when all the iterators are in motion in a loop) , i.e., we don't get `ConcurrentModificationException`? – Aakash Verma Jul 06 '16 at 04:59
  • Exactly, but this is not a solution to a problem, it is just by design. E.g. ConcurrentHashMap will iterate concurrently and you will see some of the edits other threads made (insertions and deletions) - and is says in doc that it will not fail with ConcurrentModificationException. Other collections have other semantics. – river Jul 06 '16 at 09:05
0

It does not mean multiple thread can remove data using iterator.remove().

If you want to achieve it you need to use Synchronized type of collections. Even in that case you should not try to use the same iterator in two threads. If you have two threads that need to remove entry, then they each should have their own iterators.

sauumum
  • 1,638
  • 1
  • 19
  • 36
  • What if they do have their own and they are iterating during the same time interval? I think you didn't get my question. Let me rephrase it. If two or more threads having their own individual iterators execute *iterator.remove()* **either at the same instant or anytime during which each of those iterators are traversing their collections together**, would it cause `ConcurrentModificationException`? – Aakash Verma Jul 06 '16 at 05:10