常见线上问题之ConcurrentModificationException
来源:http://www.tudoupe.com时间:2022-07-03
并行修改异常通常称为并行修改异常
什么是并发修改异常
官方解释如下:
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known asfail-fastiterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that this exception does not always indicate that an object has been concurrently modified by adifferentthread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.
Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw
ConcurrentModificationExceptionon a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness:ConcurrentModificationExceptionshould be used only to detect bugs.
可能意味着,当一个方法监测不允许修改的对象正在同时修改时(修改集合结构),就会报这个异常。这种例外在单行和多行运行环境中都是可能的。当一个线程在集合上重复时,通常, 不能再对集合进行线性修改.通常在这些情况下,迭代的结果不确定。如果这种行为被检测出来,一些迭代器实现(包括由JRE提供的所有一般集合实现)可能选择放弃这个例外。
执行操作的迭代器称为快速失败迭代器.因为迭代器很快就完全失败了,而不是在将来某个时候冒着任意不确定的风险。迭代器的快速故障行为不能保证.因为一般来说,不能对非同步同时修改的发生作出任何严格的保证。快速失败的操作将尽其所能地排除并行修改异常.
快速失败和安全失败
这里需要解释故障快速和故障安全
fail-fast
官方解释是:
当迭代器被创建时,除了可以改变集合结构的迭代器本身的方法(除去)之外,其他因素如集合结构的变化,ConcurrentModificationException例外被抛出。
因此,只要集合结构被更改,它就会抛出同时修改的例外,即集合结构的所谓修改,例如插入和删除一个集合是结构性变化,但是如果集合中的元素被修改,它不是结构性变化。
fail-safe
与失败快相比,当集合的结构发生变化时,故障安全机制将从原始收集中复制数据,然后,你复制的资料的副本经过,」 《書名錄》卷一。但相应的救济有两个缺点:
复制需要额外的时间和空间
无法保证概览的最新内容
迭代器与循环器之间的区别
首先,在循环中,循环是基于每个集合为集合类提供的方法在集合中添加、删除和修改对象。
迭代器是Java提供的序列类型数据结构的统一模型。不要担心底部结构如何。但当迭代器穿过元素时,除了查看之外,只能删除操作.但请注意这里的元素的删除,使用集合的移除方法去除元素,可以触发同时修改的例外,但是如果迭代器的除去不行;
为什么会发生并发修改异常&如何解决
事实上,有几个事情已经说过,比如触发并发的修改异常,分成单线程和多个线程
单线程运行环境
解决方案也很简单:在一个单个线程环境中,您可以将 ArrayList设置转换为 CopyOnWriteArrayList,或者它可以通过重复过渡来删除,同时修改可以避免例外.
CopyOnWriteArrayList:即,fail-safe
迭代器遍历
但迭代器过渡性删除的效率要高得多,因此第一种方法一般不推荐
多线程运行环境
在多向环境中,更改ArrayList为CopyOnWriteArrayList也可以避免多向环境中的这种异常,这意味着fail-safe也是有用的,但迭代过渡失效。
在线问题与同时修改的例外发生,其中许多被HashMap及其扩展类触发,这次可以转换为线程安全的HashMap类,并行网格地图,在此情况下, 可以避免并行修改例外.
参考:
https://zhuanlan.zhihu.com/p/37345813
相关新闻
- 2023-04-16 2台电脑怎么共享(2台电脑怎么共享
- 2023-04-16 主板检测卡代码(电脑主板检测卡代
- 2023-04-16 dnf未响应(dnf未响应老是上不去)
- 2023-04-16 ppoe(pppoe拨号上网)
- 2023-04-16 网速不稳定(网速不稳定是路由器的
- 2023-04-16 wds状态(Wds状态成功)
- 2023-04-16 光标键(光标键不动了怎么办)
- 2023-04-16 电脑提速(电脑提速100倍的方法)
- 2023-04-16 切换用户(切换用户怎么切换回来
- 2023-04-16 数据包是什么(产品数据包是什么
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
