Does anyone know why should I (for example) put my List inside Collections.syncrhonizedList()
instead of Collections.synchronizedCollection()
? Do they work the same? Same applies to Map,Set.
Another thing. Why there isn't Collections.synchronizedQueue()
?
When you are using
synchronizedCollection()
to synchronize anArrayList
, the list-specific methods are not synchronized, so the more concrete method, the better. Beware that methodCollections.synchronizedList()
will synchronize all the accesses to the backed list except while iterating which still needs to be done within a synchronized block with the synchronized List instance as object's monitor. One more thing, when you create synchronized collection usingCollections.synchronizedCollection
like this :Then, according to documentation, the returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object's equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.