Robert

78%
Flag icon
Map<K, V> m = Collections.synchronizedMap(new HashMap<>()); Set<K> s = m.keySet();  // Needn't be in synchronized block     ... synchronized(m) {  // Synchronizing on m, not s!
Robert
What happens if the map is modified between `m.keySet()` and `synchronized(m)`?
Robert
· Flag
Robert
Because the keySet is just a view, and so it's okay if the "set" changes between when you "fetch" it and use it.
Effective Java
Rate this book
Clear rating
Open Preview