Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when we pass hashtable inside Collections.synchronizedMap()

Today I have asked a question in my interview. The question is that Collections.synchronizedMap() is used to synchronize the map which are by default not thread safe like hashmap. His question is but we can pass any kind of map inside this method. So what is the effect when we pass a hashtable inside this method because hashtable is by default synchronized.

like image 650
Buzzer Avatar asked Nov 07 '14 11:11

Buzzer


3 Answers

The behavior of the map will be the same, but the performance will be affected, because each method will acquire two synchronization locks instead of one.

For example, consider calling the method size() on the resulting map. The implementation in the Collections.SynchronizedMap class looks like this:

public int size() {
    synchronized(mutex) {return m.size();} // first lock
}

... where, m.size() calls the implementation in Hashtable:

public synchronized int size() { // second lock
    return count;
}

The first lock object is the mutex field in SynchronizedMap. The second lock is implicit - the Hashtable instance itself.

like image 52
Natix Avatar answered Oct 19 '22 23:10

Natix


You would have two synchronization levels: one at the level of the synchronized map itself, implemented by a mutex object, and one at the level of the wrapped instance:

public boolean isEmpty() {
    // first level synchronization
    synchronized(mutex) {
        // second level synchronization if c is a Hashtable
        return c.isEmpty();
    }
}

The additional synchronization is not needed and can lead to lower performance.

Another effect is that you won't be able to use API from Hashtable like Hashtable#elements since the wrapped collection is now strictly a Map instance.

like image 27
M A Avatar answered Oct 20 '22 00:10

M A


It will get wrapped into a SynchronizedMap, from java.util.Collections:

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
    return new SynchronizedMap<>(m);
}

The synchronizedMap() method does not distinguish between the types of Maps passed into it.

like image 23
Icewind Avatar answered Oct 20 '22 00:10

Icewind