Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ConcurrentHashMap analogy to CopyOnWriteArrayList

Tags:

java

I use CopyOnWriteArrayList quite alot. This is especially true when

  • Threads perform a lot of read
  • Threads perform a little of write

However, I will use Collections.synchronizedList() when

  • Threads perform a little of read
  • Threads perform a lot of write

This is because according to CopyOnWriteArrayList Java Doc

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

This is ordinarily too costly, ...

When comes to ConcurrentHashMap, I was wondering can I still apply the same logic in choosing ConcurrentHashMap over Collections.synchronizedMap()?

Does ConcurrentHashMap make a fresh copy of underlying data structure, every-time I perform write operation? Will it perform badly than Collections.synchronizedMap, if there is more write operation than read?

like image 366
Cheok Yan Cheng Avatar asked Apr 11 '13 07:04

Cheok Yan Cheng


People also ask

What is difference between Synchronizedmap and ConcurrentHashMap?

ConcurrentHashMap allows performing concurrent read and write operation. Hence, performance is relatively better than the Synchronized Map. In Synchronized HashMap, multiple threads can not access the map concurrently. Hence, the performance is relatively less than the ConcurrentHashMap.

Is CopyOnWriteArrayList concurrent?

CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java.

What is CopyOnWriteArrayList?

CopyOnWriteArrayList is a thread-safe variant of ArrayList where operations which can change the ArrayList (add, update, set methods) creates a clone of the underlying array. CopyOnWriteArrayList is to be used in a Thread based environment where read operations are very frequent and update operations are rare.

Does ConcurrentHashMap use Read write lock?

Atomic operations: All operations on the ConcurrentHashMap are atomic, which means you can safely perform concurrent data access. Lock-free: The ConcurrentHashMap is designed to be lock-free, which means that there is no need to acquire a lock in order to read or write data.


2 Answers

No, ConcurrentHashMap does not make a fresh copy of underlying data structure.

ConcurrentHashMap is a segmented map, number of segments are based on concurrency level. And when you write to a segment, then it gets locked until write is complete.

like image 107
denis.solonenko Avatar answered Sep 24 '22 02:09

denis.solonenko


ConcurrentHashMap is almost always the right one to use as it has better performance and more useful API (you can avoid the check-then-set thread problem) than its counterparts
It uses lock stripping for finer grained access and does not copy the map.
The only application where you should not use a ConcurrentHashMap is when you would need to lock the map for exclusive access.

like image 27
Cratylus Avatar answered Sep 25 '22 02:09

Cratylus