[英]scala MutableList when foreach add element why not throw Exception
when I try scala list, I found a question. 当我尝试scala列表时,我发现了一个问题。 when I use a MutableList, just want to try append a element in foreach (I know this is a bad operation): 当我使用MutableList时,只想尝试在foreach中附加一个元素(我知道这是一个错误的操作):
import scala.collection.mutable.MutableList
val tts = MutableList("Hello World~")
tts.foreach { t =>
tts += "Hello World~"
println(t)
}
Console Output: 控制台输出:
Hello World~
Hello World~
My question is why the above code not throw a ConcurrentModificationException
or output normaly? 我的问题是,为什么上面的代码不抛出ConcurrentModificationException
或正常输出?
Throwing the ConcurrentModificationException
on modifications during iteration is generally called fail-fast semantics. 在迭代期间将ConcurrentModificationException
抛出修改时,通常称为快速失败语义。 Scala collection classes normally do not throw ConcurrentModificationException
s because: Scala集合类通常不会抛出ConcurrentModificationException
因为:
Because of this reasons, Scala just documents that concurrent modifications are not allowed, rather than striving for fail-fast and ending up with a leaky abstraction. 由于这个原因,Scala仅记录了不允许进行并发修改,而不是努力实现快速失败并以泄漏抽象告终。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.