Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

concurrenthasmap and ConcurrentModificationException

Posted on 2014-03-26
5
Medium Priority
?
228 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 36

Assisted Solution

by:mccarl
mccarl earned 1000 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 28

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 28

Accepted Solution

by:
dpearson earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

916 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