9

Code:

I have a HashMap

private Map<K, V> map = new HashMap<>();

One method will put K-V pair into it by calling put(K,V).

The other method wants to extract a set of random elements from its values:

int size = map.size();    // size > 0
V[] value_array = map.values().toArray(new V[size]);
Random rand = new Random();
int start = rand.nextInt(size); int end = rand.nextInt(size);
// return value_array[start .. end - 1]

The two methods are called in two different concurrent threads.


Error:

I got a ConcurrentModificationException error:

at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
at java.util.HashMap$ValueIterator.next(Unknown Source)
at java.util.AbstractCollection.toArray(Unknown Source)

It seems that the toArray() method in one thread is actually iterating over the HashMap and a put() modification in other thread occurs.

Question: How to avoid "ConcurrentModificationException" while using HashMap.values().toArray() and HashMap.put() in concurrent threads?
Directly avoiding using values().toArray() in the second method is also OK.

2
  • 1
    Execute the code that accesses the map in a synchronize block: synchronize(map){...} Commented Oct 29, 2014 at 2:38
  • 1
    synchronize(map){..} should work (if you apply it everywhere). Collections.synchronizedMap won't work. see docs.oracle.com/javase/7/docs/api/java/util/… Commented Oct 29, 2014 at 3:15

2 Answers 2

6

You need to provide some level of synchronization so that the call to put is blocked while the toArray call is executing and vice versa. There are three two simple approaches:

  1. Wrap your calls to put and toArray in synchronized blocks that synchronize on the same lock object (which might be the map itself or some other object).
  2. Turn your map into a synchronized map using Collections.synchronizedMap()

    private Map<K, V> map = Collections.synchronizedMap(new HashMap<>());
    

  3. Use a ConcurrentHashMap instead of a HashMap.

EDIT: The problem with using Collections.synchronizedMap is that once the call to values() returns, the concurrency protection will disappear. At that point, calls to put() and toArray() might execute concurrently. A ConcurrentHashMap has a somewhat similar problem, but it can still be used. From the docs for ConcurrentHashMap.values():

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

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

6 Comments

@Thilo - Right. Three approaches. :)
@Thilo Thanks. However, I have read some remarks that ConcurrentHashMap does not necessarily solve ConcurrentModificationException (but fail to find the source now). Why does it work in this/my case?
You need values() to work multi-threaded, and the Javadoc says "The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction."
"may (but is not guaranteed to)". If you need stricter assurances, add synchronized blocks to all your HashMap accesses instead.
synchronizedMap most likely won't work, see the remarks about iterating at docs.oracle.com/javase/7/docs/api/java/util/…
|
2

I would use ConcurrentHashMap instead of a HashMap and protect it from concurrent reading and modification by different threads. See the below implementation. It is not possible for thread 1 and thread 2 to read and write at the same time. When thread 1 is extracting values from Map to an array, all other threads that invoke storeInMap(K, V) will suspend and wait on the map until the first thread is done with the object.

Note: I do not use synchronized method in this context; I do not completely rule out synchronized method but I would use it with caution. A synchronized method is actually just syntax sugar for getting the lock on 'this' and holding it for the duration of the method so it can hurt throughput.

private Map<K, V> map = new ConcurrentHashMap<K, V>();

// thread 1
public V[] pickRandom() {
    int size = map.size();    // size > 0
    synchronized(map) {
        V[] value_array = map.values().toArray(new V[size]);
    }
    Random rand = new Random();
    int start = rand.nextInt(size); 
    int end = rand.nextInt(size);
    return value_array[start .. end - 1]
}

// thread 2
public void storeInMap(K, V) {
    synchronized(map) {
        map.put(K,V);
    }
}

3 Comments

Why the syncronized blocks if you're already using a ConcurrentHashMap?
Should the synchronized blocks be used while using ConcurrentHashMap?
the point of ConcurrentHashMap is to not use synchronization.

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.