1
從javadocs中Collections.synchonizedMap和同步
Map m = Collections.synchronizedMap(new HashMap());
...
Set s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
這裏爲什麼迭代已經封閉在同步塊?
這是否意味着每次我們需要遍歷synchronizedMap時,我們都必須將迭代器放在synchronized塊中?