Explain synchronization of collections when iterators are used?

Viewed 18609

I understand that collections like the Hashtable are synchronized, but can someone explain to me how it works, and at what point(s) access is restricted to concurrent calls? For example, let's say I use some iterators like this:

Hashtable<Integer,Integer> map = new Hashtable<Integer,Integer>();

void dosomething1(){
    for (Iterator<Map.Entry<Integer,Integer>> i = map.entrySet().iterator(); i.hasNext();){
        // do something
    }
}
void dosomething2(){
    for (Iterator<Map.Entry<Integer,Integer>> i = map.entrySet().iterator(); i.hasNext();){
        // do something
        // and remove it
        i.remove();
    }
}
void putsomething(int a, int b){
    map.put(a,b);
}
void removesomething(int a){
    map.remove(a);
}
var clear(){
    map = new Hashtable<Integer,Integer>();
}

Can someone please explain if there are any pitfalls with me calling these functions at random from different threads? How does the iterator, in particular, do its synchronization, especially when it is using entrySet(), which would seem to also require synchronization? What happens if clear() is called while one of the loops is in progress? What if removesomething() removes an item that is not yet processed by a concurrent loop in dosomething1() ?

Thanks for any help!

2 Answers

I understand that collections like the Hashtable are synchronized

The HashTable's entry set uses a SynchronizedSet which is a type of SynchronizedCollection.

If you modify any collection synchronized or not while using an iterator on it, the iterator will throw a ConcurrentModificationException.

An iterator is an Object that acts on a collection, being given the collection's state during construction. This lets you decide when you want to see the next item in the collection, if ever. You must use an iterator on a collection you know won't be modified, or only plan to modify using the iterator.

The reason ConcurrentModificationException is thrown is because of a check iterators make on the collection's current modification count, if it doesn't match the expected value, the exception is thrown. All collections increment a modification count variable each time something is added or removed.

How does the iterator, in particular, do its synchronization, especially when it is using entrySet()

So the iterator doesn't do synchronization and is not safe to use when you expect the collection to be modified by other threads, (or the current thread outside of the iterator).

However, SynchronizedCollection does provide a way to go though the collection synchronously. Its implementation of the forEach method is synchronized.

public void forEach(Consumer<? super E> consumer)

Just keep in mind, forEach uses an enhanced for loop which uses an iterator internally. This means forEach is only for reviewing the collection's contents, not for modifying it while looking through. Otherwise ConcurrentModificationException will be thrown.

can someone explain to me how it works, and at what point(s) access is restricted to concurrent calls

SynchronizedCollection causes threads to take turns accessing the collection if they want to use the synchronized methods such as (add, remove, forEach).

It works by introducing a synchronized block similar to how it's shown in the following code:

public boolean add(Object o) {
  synchronized(this) {
  super.add(o);
  }
}

A synchronized block is introduced around all of the operations you can perform on the collection except for the following methods:

iterator(), spliterator(), stream(), parallelStream()

Java Official Documentation

Related