Solved

concurrenthasmap and ConcurrentModificationException

Posted on 2014-03-26
5
214 Views
Last Modified: 2014-04-29
Hi,

How is ConcurrentHashMap different from Map and HashMap. Waht are advantages, disadvantages, practical uses of each of them. what is ConcurrentModificationException.


please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 2
  • 2
5 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 39957773
How is ConcurrentHashMap different from Map and HashMap
Firstly, Map isn't part of this comparison, because Map is an interface that the other two both implement. It isn't a separate entitiy in it's own right. So this should just be...

How is ConcurrentHashMap different from HashMap
ConcurrentHashMap is basically the same as HashMap but it allows access to it's various method, get, put, etc and to iterate over it from multiple threads at the same time. A HashMap can't be used in this way, it either should be used from only one thread, otherwise access to it's methods should be synchronized in some way.

what is ConcurrentModificationException
ConcurrentModificationException is an exception that a HashMap may throw if one thread is iterating over the map while another thread attempts to make certain changes to the map. It throws this instead of the alternative possibility where the iteration may fail in some other unknown way.

The ConcurrentHashMap never throws this exception. Again, it is coded to handle multiple threads accessing it, and so it can do things to work around this multiple thread access and always iterate in a correct way.
0
 
LVL 27

Expert Comment

by:dpearson
ID: 39958191
I agree with everything mccarl says here, but just wanted to add one last point...which is that ConcurrentHashMap not only allows multiple threads to access a shared hash map, it supports them all doing this incredibly efficiently.  It's much more efficient to use than using a synchronized HashMap.

It's really an amazing addition to the Java language.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40020385
Hashtable also allows multiple threads. So to use ConcurrentHashMap  do we need Hash map for sure. Cannot we use ConcurrentHashMap  independently. Is there is any simple example on ConcurrentHashMap usage. please advise
0
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 40020932
There's really nothing special required to use ConcurrentHashMap compared to a regular HashMap.  You generally use it just like any other Map.

ConcurrentHashMap<Integer, String> myMap = new ConcurrentHashMap<Integer, String>() ;
myMap.put(10, "Adam") ;
myMap.put(20, "Eve") ;

String getPerson = myMap.get(10) ; // Will return Adam

There are a few extra methods available (e.g. putIfAbsent) but much of the time you can just use it like a normal map.

The difference between ConcurrentHashMap or HashMap or Hashtable is just when you write that same set of code in different threads.

For HashMap or Hashtable, the thread will either cause an exception in the map or (if you use a synchronized version) each thread will block the others making them run correctly but slowly.

For ConcurrentHashMap, the threads won't cause an exception and will run faster.

So just use ConcurrentHashMap when you need a map and multiple threads are involved.

Hope that helps,

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40030695
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question