[concurrency-interest] util.concurrent 1.3.4

Elias Ross eross at m-Qube.com
Mon Nov 7 18:25:10 EST 2005


I know there is an existing bug with 

ConcurrentReaderHashMap.keySet().toArray()

but one interesting way I've come across this is with this sort of code:

ConcurrentReaderHashMap cr;
List l = new ArrayList();

  l.addAll(cr.keySet());

What ends up happening is "null" entries end up getting added to the
ArrayList.  This is because, despite what the JavaDoc says:
        
        Appends all of the elements in the specified Collection to the
        end of this list, in the order that they are returned by the
        specified Collection's Iterator.
        
The code for ArrayList calls "toArray" on the set and ignores the
iterator.

    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        ...
    }




More information about the Concurrency-interest mailing list