登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
Gitee 2025 年度开源项目评选中
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
3
Star
45
Fork
21
DreamCoders
/
CoderGuide
代码
Issues
1169
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
Java集合的快速失败机制 “fail-fast”?
待办的
#IAJKQ7
陌生人
拥有者
创建于
2024-08-13 10:04
<p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">是</span><span style="color: rgb(51, 51, 51);">java</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">集合的一种错误检测机制,当多个线程对集合进行结构上的改变的操作 时,有可能会产生</span><span style="color: rgb(51, 51, 51);"> fail-fast </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">机制。 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">例如:假设存在两个线程(线程</span><span style="color: rgb(51, 51, 51);">1</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">、线程</span><span style="color: rgb(51, 51, 51);">2</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">),线程</span><span style="color: rgb(51, 51, 51);">1</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">通过</span><span style="color: rgb(51, 51, 51);">Iterator</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">在遍历集合</span><span style="color: rgb(51, 51, 51);">A</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">中 的元素,在某个时候线程</span><span style="color: rgb(51, 51, 51);">2</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">修改了集合</span><span style="color: rgb(51, 51, 51);">A</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的结构(是结构上面的修改,而不是简 单的修改集合元素的内容),那么这个时候程序就会抛出</span><span style="color: rgb(51, 51, 51);">ConcurrentModificationException </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">异常,从而产生</span><span style="color: rgb(51, 51, 51);">fail-fast</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">机制。 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">原因:迭代器在遍历时直接访问集合中的内容,并且在遍历过程中使用一个</span><span style="color: rgb(51, 51, 51);"> modCount </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">变量。集合在被遍历期间如果内容发生变化,就会改变</span><span style="color: rgb(51, 51, 51);">modCount </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的值。每当迭代器使用</span><span style="color: rgb(51, 51, 51);">hashNext()/next()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">遍历下一个元素之前,都会检测</span><span style="color: rgb(51, 51, 51);"> modCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">变量是否为</span><span style="color: rgb(51, 51, 51);">expectedmodCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,是的话就返回遍历;否则抛出异常,终止遍历。</span></p><h4><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">解决办法: </span></h4><p><span style="color: rgb(51, 51, 51);">1. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">在遍历过程中,所有涉及到改变</span><span style="color: rgb(51, 51, 51);">modCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值得地方全部加上</span><span style="color: rgb(51, 51, 51);"> synchronized</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">。 </span></p><p><span style="color: rgb(51, 51, 51);">2. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">使用</span><span style="color: rgb(51, 51, 51);">CopyOnWriteArrayList</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">来替换</span><span style="color: rgb(51, 51, 51);">ArrayList </span></p>
<p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">是</span><span style="color: rgb(51, 51, 51);">java</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">集合的一种错误检测机制,当多个线程对集合进行结构上的改变的操作 时,有可能会产生</span><span style="color: rgb(51, 51, 51);"> fail-fast </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">机制。 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">例如:假设存在两个线程(线程</span><span style="color: rgb(51, 51, 51);">1</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">、线程</span><span style="color: rgb(51, 51, 51);">2</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">),线程</span><span style="color: rgb(51, 51, 51);">1</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">通过</span><span style="color: rgb(51, 51, 51);">Iterator</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">在遍历集合</span><span style="color: rgb(51, 51, 51);">A</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">中 的元素,在某个时候线程</span><span style="color: rgb(51, 51, 51);">2</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">修改了集合</span><span style="color: rgb(51, 51, 51);">A</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的结构(是结构上面的修改,而不是简 单的修改集合元素的内容),那么这个时候程序就会抛出</span><span style="color: rgb(51, 51, 51);">ConcurrentModificationException </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">异常,从而产生</span><span style="color: rgb(51, 51, 51);">fail-fast</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">机制。 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">原因:迭代器在遍历时直接访问集合中的内容,并且在遍历过程中使用一个</span><span style="color: rgb(51, 51, 51);"> modCount </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">变量。集合在被遍历期间如果内容发生变化,就会改变</span><span style="color: rgb(51, 51, 51);">modCount </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的值。每当迭代器使用</span><span style="color: rgb(51, 51, 51);">hashNext()/next()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">遍历下一个元素之前,都会检测</span><span style="color: rgb(51, 51, 51);"> modCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">变量是否为</span><span style="color: rgb(51, 51, 51);">expectedmodCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,是的话就返回遍历;否则抛出异常,终止遍历。</span></p><h4><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">解决办法: </span></h4><p><span style="color: rgb(51, 51, 51);">1. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">在遍历过程中,所有涉及到改变</span><span style="color: rgb(51, 51, 51);">modCount</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值得地方全部加上</span><span style="color: rgb(51, 51, 51);"> synchronized</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">。 </span></p><p><span style="color: rgb(51, 51, 51);">2. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">使用</span><span style="color: rgb(51, 51, 51);">CopyOnWriteArrayList</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">来替换</span><span style="color: rgb(51, 51, 51);">ArrayList </span></p>
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
Java
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
未关联
master
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册