Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

concurrenthasmap and ConcurrentModificationException

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 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