In a multi-thread module, I don't care whether it is thread-safely writing over a java.util开发者_如何学编程.Collection
(e.g. java.util.Set
) object. What it does is removing some independent elements by multiple threads, which may perform removal on the same element at the same time. Then, what happens? That element is removed, or any exception is thrown?
Kejia
It is even possible that an ConcurrentModificationException is thrown, when you remove different objects. That depends on the implementation of the Set you are using: The remove() method may iterate over the set. Or worse: It may modifies some other internal variables and leave the set in an inconsistent state without throwing any exception.
There is a really easy solution:
Set safeSet = Collections.synchronizedSet(set);
You still need to pay attention if you want to iterate over it, as described in the apidoc: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedSet%28java.util.Set%29 but all the other methods (including remove) are fine.
The results are unpredictable. The internal structure of the Set
may be left in an inconsistent and unusable state.
'ConcurrentModificationException' if you are lucky. Chaos if not. Either use Collections.synchronized* or synchronize on some other object, don't do this.
java.util.Set is only an interface. The behaviour depends on the implementation of the Set interface. You must not access a HashSet with multiple threads as the behaviour is not guaranteed, although it is likely to throw ConcurrentModificationException if you do this.
You might not care, because you think that the Set would retain its integrity if you do something like that. But it won't.
You either need Collections.newSetFromMap(new ConcurrentHashMap<E, Boolean>());
or to use some syncronization. Removal isn't the only problem here. You could also end up with duplicates in the Set or who knows what else if you just use a HashSet.
Given a proper Set implementation designed to handle threading issues, then one thread will remove the item and the other will find the item not there, which can be determined by the return value of the remove method on the Set.
Note that if you are iterating over the set, you have to use some external synchronization if you are removing elements during the iteration.
精彩评论