Solved

concurrenthasmap and ConcurrentModificationException

Posted on 2014-03-26
5
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 67
ejb entity bean example 2 59
Glassfish admin console not working 1 88
JDBC performance 8 35
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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