The java.util.Collections
interface's documentation [API 2006] Java Tutorials, Wrapper Implementations [Java Tutorials], warns about the consequences of failing to synchronize on an accessible collection object when iterating over its view:
It is imperative that the user manually synchronize on the returned map
Map
when iterating over any of its collection views.... Failure to followCollection
views rather than synchronizing on theCollection
view itself.
Disregarding this advice may result in
...
nondeterministic behavior.
Any class that uses a collection view rather than the backing collection as the lock object may end up with two distinct locking strategies. When the backing collection is accessible to multiple threads, the class that locked on the collection view has violated the thread-safety properties and is unsafe. Consequently, programs that both require synchronization while iterating over collection views and have accessible backing collections must synchronize on the backing collection; synchronization on the view is a violation of this rule.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
ThreadSafe |
| CCE_CC_SYNC_ON_VIEW | Implemented |
Bibliography
[API 2006] | "Class Collections" |
Issue Tracking
...