Which Java Collections are synchronized(thread safe), which are not?

前端 未结 10 2108
礼貌的吻别
礼貌的吻别 2020-12-16 11:05

Which Java Collections are synchronized, which are not?

Example: HashSet is not synchronized

10条回答
  •  粉色の甜心
    2020-12-16 11:55

    Thread safe Collections -

    1. 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.

    1. SynchronizedHashMap

    Object level synchronization Both read and writes acquire a lock Locking the collection has a performance drawback May cause contention

    1. Vector

    2. HashTable

    3. CopyOnWriteArrayList

    4. CopyOnWriteArraySet

    5. Stack

    Rest all are not thread safe

提交回复
热议问题