collection中的fast-fail机制

collection中的fast-fail机制

摘自[java Generics and Collections]

The policy of the iterators for the Java 2 collections is to fail fast, as described in Section 11.1: every time they access the backing collection, they check it for structural modification (which, in general, means that elements have been added or removed from the collection). If they detect structural modification, they fail immediately, throwing ConcurrentModificationException rather than continuing to attempt to iterate over the modified collection with unpredictable results. Note that this fail-fast behavior is provided to help find and diagnose bugs; it is not guaranteed as part of the collection contract

在Java 2中的集合中的iterator使用的是fail fast机制,在Section11.1中描述:在每次访问collection时,迭代器会校验该collection的结构是否被修改过(一般来说,就是collection中的元素被添加或者被移除),如果检测到collection的结构被修改过,迭代器立即失败,抛出ConcurrentModificationException异常,而不是继续尝试去迭代遍历这个被修改过的collection从而返回不可预测的结果。注意,这个fail fast机制是提供用来帮助和诊断bugs,它不能作为保证集合的一种约定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值