Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safe Hash Map?

I am writing an application which will return a HashMap to user. User will get reference to this MAP. On the backend, I will be running some threads which will update the Map.

What I have done so far?


I have made all the backend threads so share a common channel to update the MAP. So at backend I am sure that concurrent write operation will not be an issue.


Issues I am having


  1. If user tries to update the MAP and simultaneously MAP is being updated at backend --> Concurrent write operation problem.
  2. If use tries to read something from MAP and simultaneously MAP is being updated at backend --> concurrent READ and WRITE Operation problem.

Untill now I have not face any such issue, but i m afraid that i may face in future. Please give sugesstions.

I am using ConcurrentHashMap<String, String>.

like image 477
user381878 Avatar asked Jul 11 '10 09:07

user381878


People also ask

Are hash maps thread-safe?

HashMap is non-synchronized. It is not thread-safe and can't be shared between many threads without proper synchronization code whereas Hashtable is synchronized.

How do I make a HashMap thread-safe?

You can make HashMap thread safe by wrapping it with Collections. synchronizedMap() . What's the difference? @naXa ConcurrentHashMap allows concurrent access and a synchronized one doesn't.

Is Java map put thread-safe?

Java HashMap is not thread-safe and hence it should not be used in multithreaded applications.

What is difference between ConcurrentHashMap and HashMap?

HashMap is non-Synchronized in nature i.e. HashMap is not Thread-safe whereas ConcurrentHashMap is Thread-safe in nature. HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously.


2 Answers

You are on the right track using ConcurrentHashMap. For each point:

  1. Check out the methods putIfAbsent and replace both are threadsafe and combine checking current state of hashmap and updating it into one atomic operation.
  2. The get method is not synchronized internally but will return the most recent value for the specified key available to it (check the ConcurrentHashMap class Javadoc for discussion).

The benefit of ConcurrentHashMap over something like Collections.synchronizedMap is the combined methods like putIfAbsent which provide traditional Map get and put logic in an internally synchronized way. Use these methods and do not try to provide your own custom synchronization over ConcurrentHashMap as it will not work. The java.util.concurrent collections are internally synchronized and other threads will not respond to attempts at synchronizing the object (e.g. synchronize(myConcurrentHashMap){} will not block other threads).

like image 190
krock Avatar answered Oct 17 '22 03:10

krock


Side Note:

You might want to look into the lock free hash table implementation by Cliff Click, it's part of the Highly Scalable Java library

(Here's a Google Talk by Cliff Click about this lock free hash.)

like image 8
miedwar Avatar answered Oct 17 '22 02:10

miedwar