In Java there are
concurrent collections (e.g.
java.util.concurrent.CopyOnWriteArrayList
)synchronized collections (e.g.
Collections.synchronizedList(List<T> list)
)
My insights so far:
- both types are thread safe regarding invoking their operations (
add()
,get()
and so on) - BUT: If you iterate over a Collection from type 2) it isn't thread safe at all (compare this answer).Whereas iterating over a Collection of type 1) is thread safe.
Are there any other differences? And why has Java both types of collections? Wouldn't it be clearer to only have one type?