遍历同步收集 [英] Iterating over synchronized collection
本文介绍了遍历同步收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里问了一个关于在Vector
上进行迭代的问题,并为我提供了一些好的解决方案.但是我读到另一种更简单的方法.我想知道这是否是很好的解决方案.
I asked here a question about iterating over a Vector
, and I have been answered with some good solutions. But I read about another simpler way to do it. I would like to know if it is good solution.
synchronized(mapItems) {
Iterator<MapItem> iterator = mapItems.iterator();
while(iterator.hasNext())
iterator.next().draw(g);
}
mapItems是一个同步的集合:向量.这是否使从ConcurrentModificationException
进行Vector
的迭代安全?
mapItems is a synchronized collection: Vector. Is that make the iterating over the Vector
safe from ConcurrentModificationException
?
推荐答案
是的,它将使ConcurrentModificationException
成为安全的方法,而实际上一切都是单线程的.
Yes, it will make it safe from ConcurrentModificationException
at the expense of everything essentially being single-threaded.
这篇关于遍历同步收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文