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.

hengxin
  • 1,867
  • 2
  • 21
  • 42
  • 1
    Execute the code that accesses the `map` in a synchronize block: `synchronize(map){...}` – Titus Oct 29 '14 at 02:38
  • 1
    `synchronize(map){..}` should work (if you apply it everywhere). Collections.synchronizedMap won't work. see http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap%28java.util.Map%29 – Thilo Oct 29 '14 at 03:15

2 Answers2

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @Thilo - Right. Three approaches. :) – Ted Hopp Oct 29 '14 at 02:43
  • @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? – hengxin Oct 29 '14 at 02:49
  • 1
    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." – Thilo Oct 29 '14 at 02:52
  • "may (but is not guaranteed to)". If you need stricter assurances, add synchronized blocks to all your HashMap accesses instead. – Thilo Oct 29 '14 at 02:53
  • `synchronizedMap` most likely won't work, see the remarks about iterating at http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap%28java.util.Map%29 – Thilo Oct 29 '14 at 02:55
  • @Thilo The weak guarantee is enough for my task. I will try `ConcurrentHashMap`. Thanks for the explanations and the great insights into `synchronizedMap` and `ConcurrentHashMap.values()` from both of you. – hengxin Oct 29 '14 at 03:02
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);
    }
}
Danny Ho
  • 21
  • 1