Block read operation of ConcurrentHashMap in multithreaded environment
Block read operation of ConcurrentHashMap in multithreaded environment
I have a ConcurrentHashMap. When thread t1 will do a write operation on it then I want thread t2 cannot do any read operation on it. The read operation should then be blocked. How will I achieve this? I cannot use HashMap and synchronize it.
There is no real point in using a ConcurrentHashMap then. A simple HashMap is sufficient. Synchronize the accesses using a ReadWriteLock. docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/…
– JB Nizet
Jun 30 at 5:31
Why use a
ConcurremtHashMap if you don't want it concurrent?– EJP
Jun 30 at 5:37
ConcurremtHashMap
1 Answer
1
ConcurrentHashMap provides its own synchronization, so you do not have to synchronize accesses to it explicitly.
That's only true if you don't need any atomic composite operation, like updating two entries atomically, or iterating on the map atomically, for example.
– JB Nizet
Jun 30 at 8:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Then can you synchronize on the ConcurrentHashMap itself?
– user7
Jun 30 at 5:31