Solved

concurrenthasmap and ConcurrentModificationException

Posted on 2014-03-26
5
211 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 26

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 26

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now