Search code examples
javajava.util.concurrent

is there any Concurrent LinkedHashSet in JDK6.0 or other libraries?


my code throw follow exception:

java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
        at java.util.LinkedList$ListItr.next(LinkedList.java:696)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
        at java.util.LinkedHashSet.<init>(LinkedHashSet.java:152)
        ...

I want a ConcurrentLinkedHashSet to fix it,

but I only found ConcurrentSkipListSet in java.util.concurrent,this is TreeSet, not LinkedHashSet

any easies way to get ConcurrentLinkedHashSet in JDK6.0?

thanks for help :)


Solution

  • A ConcurrentModificationException has nothing to do with concurrency in the form you're thinking of. This just means that while iterating over the Collection, someone (probably your own code - that happens often enough ;) ) is changing it, i.e. adding/removing some values.

    Make sure you're using the Iterator to remove values from the collection and not the collection itself.

    Edit: If really another thread is accessing the Collection at the same time, the weak synchronization you get from the standard library is useless anyhow, since you've got to block the Collection for the whole duration of the operation not just for one add/remove! I.e. something like

    synchronize(collection) {
       // do stuff here
    }