• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:Hi,

I hope you are aware of Hashtable



Don't use it.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Don't use it.

 
Krishna Kumar S
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff !!! got it.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic