2

code:

Map<Integer,DealCountUpdater> dealCountMap=new HashMap<Integer,DealCountUpdater>();

public void update(){
    for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){//line:58
        System.out.println(e.hashCode());
    }
}

when I run this code,get below exception:

java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$EntryIterator.next(HashMap.java:834)
        at java.util.HashMap$EntryIterator.next(HashMap.java:832)
        at java.util.HashMap.putAllForCreate(HashMap.java:435)
        at java.util.HashMap.<init>(HashMap.java:225)
        at org.my.tuan.count.CountUpdater.update(CountUpdater.java:58)
        at org.my.tuan._Maintainer.run(TuanSched.java:110)

this line is CountUpdater.java:58 :

for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){

I google this program,I know I can using a ConcurrentHashMap instead of a plain HashMap,

but I want to know ,why I using :

new HashMap<Integer,DealCountUpdater>(dealCountMap)

to create new Instance for HashMap,still throw ConcurrentModificationException ?

how to fix it by not using ConcurrentHashMap ?

thanks for help :)

2
  • 1
    You left out the most important part, which is the code represented by //updating.... We aren't psychic. Please add the code to the post. Also, is your program multi-threaded, and is there other activity that could alter the source map? Commented Feb 24, 2011 at 4:39
  • thasnk for help, this exception throw at CountUpdater.java:58,this line at:for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){ , I added it in my question, //updating... is nothing to do Commented Feb 24, 2011 at 4:46

2 Answers 2

6

The reason is this:

  1. You create a new hashmap(H1) by passing another hashmap(H2) in its constructor.
  2. In the constructor fo H1, it tries to iterate through elements of H2, to add in itself.
  3. While the iteration in step 2 is going on, some other thread modifies H2. Hence ConcurrentModificationException.

How to solve it without using ConcurrentHashMap?

  1. Do external synchronization
  2. Use a copy-n-write map as described here.

But I would still suggest using ConcurrentHashMap, unless you really have your reasons.

Sign up to request clarification or add additional context in comments.

1 Comment

can you give a working example for ConcurrentHaspMap is above scenario?
1

You simply cannot,or should not, modify a hashmap in a thread while another thread is iterating over it. In such cases, the HashMap iterator will throw a ConcurrentModificationException. This is called 'fail-fast' behaviour of iterators as it is clearly mentioned in the java docs. The best solution for you is to use ConcurrentHashMap. If you don't want to use that one, then you have to do away with the multi-threading. As jim pointed out, if you can give more details about your multi-threading then you might get a better solution. Also, why do you want to NOT use ConcurrentHashMap? Any particular reason?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.