Which Java Collections are synchronized(thread safe), which are not?
collections, java
Solution
Thread safe Collections -
- ConcurrentHashMap
Thread safe without having to synchronize the whole map Very fast reads while write is done with a lock No locking at the object level Uses multitude of locks.
- SynchronizedHashMap
Object level synchronization Both read and writes acquire a lock Locking the collection has a performance drawback May cause contention
Vector
HashTable
CopyOnWriteArrayList
CopyOnWriteArraySet
Stack
Rest all are not thread safe
Problem
Which Java Collections are synchronized, which are not? Example: HashSet is not synchronized