[concurrency-interest] ConcurrentModificationException not applicable to Map.values()?

Will McQueen willmcqueen at yahoo.com
Tue Dec 15 20:05:05 EST 2009

I forgot to change the subject title... it should be:
"ConcurrentModificationException not applicable to ConcurrentHashMap?"

--- On Tue, 12/15/09, Will McQueen <willmcqueen at yahoo.com> wrote:

From: Will McQueen <willmcqueen at yahoo.com>
Subject: ConcurrentModificationException not applicable to Map.values()?
To: concurrency-interest at cs.oswego.edu
Date: Tuesday, December 15, 2009, 5:01 PM


I'm puzzled why ConcurrentModificationException is not thrown in the case where I use a ConcurrentHashMap instead of HashMap. The code below is single-threaded so that ConcurrrentModificationException should definitely be thrown (as opposed to "good-faith-effort"). [Ref: JCIP "Iterators and ConcurrentModificationException"]

Below is my code that shows 2 cases -- the first where ConcurrentModificationException is not thrown (that's the case I don't understand), and the second case shows where it is thrown.

This example below shows ConcurrenHashMap, which I use in the multi-threaded version of my code. So technically I could use just a HashMap for this single-threaded demo.... but then this would not reproduce this issue. That is, puzzlingly, case #1 *does* throw the expected ConcurrentModificationException when I use a HashMap instead of
 ConcurrentHashMap (!).

public final class QuickTest {
    public static void main(String[] args) {
        /*** CASE 1 ****/
        final Map<String,String> map;
        map = new ConcurrentHashMap<String,String>();
        map.put("b", "2");
        map.put("c", "3");

        Collection<String> view = map.values();
        Iterator<String> itr = view.iterator();
        String e1 = itr.next();
        view.remove(e1); //direct
 removal through the collection, not through the iterator
        String e2 = itr.next(); //??? Why doesn't this throw ConcurrentModificationException?

        /*** CASE 2 ***/

        final List<String> list;
        list = new ArrayList<String>();

        itr = list.iterator();
        e1 = itr.next();
        list.remove(e1); //direct removal through
 the collection, not through the iterator
        e2 = itr.next(); //(throws ConcurrentModificationException, as expected)

Your insight and help is greatly appreciated. Thank you.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091215/a68b1b92/attachment-0001.html>

More information about the Concurrency-interest mailing list