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

半世苍凉 提交于 2020-01-22 23:01:54

问题


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


回答1:


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
}



回答2:


You can always create a synchronized collection with Collections.synchronizedMap(myMap);. However, trying to alter the map while you're iterating (which I'm assuming is the cause of your error) will still be a problem.

From the docs for synchronizedMap:

Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.

It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views ... Failure to follow this advice may result in non-deterministic behavior.

This is because

  • normally a concurrent collection is really guaranteeing atomic get/put but is not locking the entire collection during iteration, which would be too slow. There's no concurrency guarantee over iteration, which is actually many operations against the map.

  • it's not really concurrency if you're altering during iteration, as it's impossible to determine correct behavior - for example, how do you reconcile your iterator returning hasNext == true with deleting a (possibly the next value) from the collection?




回答3:


There is ConcurrentLinkedHashMap - https://code.google.com/p/concurrentlinkedhashmap/

You can create Set out of it with java.util.Collections.newSetFromMap(map)




回答4:


Unfortunately not. You could implement your own, wrapping a ConcurrentHashMap and a ConcurrentLinkedQueue, but this wouldn't allow you to remove values easily (removal would be O(N), since you'd have to iterate through everything in the queue) ...

What are you using the LinkedHashSet for though? Might be able to suggest alternatives...



来源:https://stackoverflow.com/questions/5290790/is-there-any-concurrent-linkedhashset-in-jdk6-0-or-other-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!