Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use Hashmap (synchronized as needed) and when to use Concurrent hashmap?

 
Krishna Kumar S
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that get and put method of hash map is not thread safe. So if multiple threads access, these methods may get interleaved and may cause hashmap to be corrupted.
so considering the below example, is it good to use hash map synchronized manually as given or is it better to use concurrent hashmap and WHY?


 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna Kumar S wrote:I understand that get and put method of hash map is not thread safe. So if multiple threads access, these methods may get interleaved and may cause hashmap to be corrupted.
so considering the below example, is it good to use hash map synchronized manually as given or is it better to use concurrent hashmap and WHY?

Class College{
private HashMap student_map = new HashMap();

//add student and id
public synchronized void add(Student s) {
student_map.put(s.getId(), s);
}

//remove student and id
public synchronized void remove(Student s){
student_map.remove(s.getId());
}

public synchronized Student get(int id){
return student_map.get(id);
}

}



The ConcurrentHashMap class uses reader writer locks in order to allow parallel reads. This is great when there are lots of readers. The ConcurrentHashMap class segments the buckets in order to allow parallel writes. This helps when there are lots of writers.

Of course, this is only useful if there are lots of parallel readers and writers. In cases where there isn't, it is just overhead.


IMO, it is too subtle for a good answer. If it is really that important to you, you will have to test it yourself. Otherwise, just use the ConcurrentHashMap class and move on.

Henry
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I hope you are aware of Hashtable (the synchronized sibling of HashMap). ConcurrentHashMap is simply (ok, not simply ) high performance Hashtable.

In synchronized HashMap:
1) While reading, complete map is locked. Thus, if 5 threads are reading from it, at any given time, only one thread would be actually reading.
2) While writing, complete map is locked. Thus, if 5 threads are writing even to different keys, at any given time, only one write operation is performed.

In ConcurrentHashMap:
1) While reading, map is not locked. Thus, if 5 threads are reading from it, all of them can simultaneously read from map.
2) While writing, only the record (key) under consideration is locked. Thus, if 5 threads are writing values of different keys, all those operations can happen simultaneously. However, if 2 threads are writing to same key, those operations are thread-safe.

ConcurrentHashMap is recommended for large Maps or large number of read-write operations because of its good scalability.

I hope this helps.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi,

I hope you are aware of Hashtable


Don't use it.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Don't use it.
 
Krishna Kumar S
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, Shivalkar and Jeff. Need few more clarifications.
Consider the same example with little modification,



In this case , lets consider both student map and student meta data map are atomic and needs to be synchronized.
Would collections.sync'dmap or concurrent hash map be required in this scenario or normal Hashmap is suffice??? (Note: Both maps are updated/accessed only from synchronized method of College class).

Thanks in Advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna Kumar S wrote:Thanks Henry, Shivalkar and Jeff. Need few more clarifications.
Consider the same example with little modification,



In this case , lets consider both student map and student meta data map are atomic and needs to be synchronized.
Would collections.sync'dmap or concurrent hash map be required in this scenario or normal Hashmap is suffice???


If the maps are always only accessed in a synced method, and if all access to a given map is through synced methods on the same object (which appears to be the case, since the maps are member variables of these objects), then you don't need any further syncing or concurrency handling in the maps themselves.

If, however, the maps can be accessed outside of that syncing, then you'll need something more, but what exactly depends on the details of your use cases.
 
Krishna Kumar S
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff !!! got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic