本文介绍了在JDK6.0或其他库中是否有任何Concurrent LinkedHashSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码抛出以下异常:

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)
        ...

我想要一个 ConcurrentLinkedHashSet 来修复它,

但我只找到 ConcurrentSkipListSet java.util.concurrent 中,这是 TreeSet ,而不是 LinkedHashSet

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

在JDK6中获取 ConcurrentLinkedHashSet 的任何简易方法。 0?

any easies way to get ConcurrentLinkedHashSet in JDK6.0?

感谢您的帮助:)

推荐答案

ConcurrentModificationException没有任何内容以您正在考虑的形式处理并发问题。这只是意味着在迭代Collection时,有人(可能是你自己的代码 - 经常发生的事情;))正在改变它,即添加/删除一些值。

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.

确保您使用Iterator从集合中删除值,而不是集合本身。

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

编辑:如果真的另一个线程同时访问Collection,那么从标准库获得的弱同步无论如何都是无用的,因为你必须阻止整个操作期间的集合不仅仅是一个添加/删除!即类似

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
}

这篇关于在JDK6.0或其他库中是否有任何Concurrent LinkedHashSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:19