简体   繁体   English

为什么并发修改异常不会在并发包中抛出?

[英]Why concurrent modification exception will not be thrown in the concurrent package?

Classes like ConcurrentHashmap will not throw this exception; 诸如ConcurrentHashmap类的类不会引发此异常; why? 为什么? What are they doing internally to avoid this exception? 他们在内部做什么以避免这种例外? How can I prevent this for all classes in the concurrent API? 如何防止并发API中的所有类都出现这种情况?

Concurrent collections implementations Iterators do not throw ConcurentModificationException because this is API's requirement, see java.util.concurrent package javadocs. 并发集合的实现迭代器不会引发ConcurentModificationException,因为这是API的要求,请参见java.util.concurrent包javadocs。 Different implementations use different techniques to achieve that, eg creating a snapshot of the state of the collection. 不同的实现使用不同的技术来实现这一目标,例如,创建集合状态的快照。 You can see JDK source code for implementation details. 您可以查看JDK源代码以获取实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM