Need to manually synchronize the Synchronized list while iteration when it could be avoided?
- 阅读更多 关于 Need to manually synchronize the Synchronized list while iteration when it could be avoided?
问题 My question is about synchronizedList method Collections Class. Javadocs say: It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } Though manually synchroniziation is not required for other methods. I looked into the source code of Collections class and found