wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes When to use Hashmap (synchronized as needed) and when to use Concurrent hashmap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "When to use Hashmap (synchronized as needed) and when to use Concurrent hashmap?" Watch "When to use Hashmap (synchronized as needed) and when to use Concurrent hashmap?" New topic
Author

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

Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Anayonkar Shivalkar wrote:Hi,

I hope you are aware of Hashtable


Don't use it.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Jeff Verdegan wrote:Don't use it.
Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 31, 2012
Posts: 12
Thanks Jeff !!! got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use Hashmap (synchronized as needed) and when to use Concurrent hashmap?
 
Similar Threads
Key in the HashMap.
newbie simple java program
update a JList based on an Array
Synchronizing a HashMap records
concurrentAccess Exception