In Collections
class, class SynchronizedMap
has two constructors. One takes only a map
instance and another with a map
and a mutex
.
SynchronizedMap(Map<K,V> m) {
this.m = Objects.requireNonNull(m);
mutex = this;
}
SynchronizedMap(Map<K,V> m, Object mutex) {
this.m = m;
this.mutex = mutex;
}
However, SynchronizedMap class is a private static class and only way to access it using provided wrapper method:
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
return new SynchronizedMap<>(m);
}
As understood from this link, the idea of the second constructor to use user-supplied mutex
other than this
. Now, since the wrapper method is the only way to get an instance of SynchronizedMap
(which takes only a map object) , what is the purpose of this second overloaded constructor?