【问题标题】:Android ConcurrentModification onDraw()Android ConcurrentModification onDraw()
【发布时间】:2012-11-15 03:19:50
【问题描述】:
我在使用 android 应用程序时遇到问题。
我有一个线程不断迭代列表或形状,更新它们的位置,有时还会从列表中删除一个项目。在线程的 while 循环结束时,它调用 postInvalidate() 来提示重新绘制。
这里是修改 ArrayList 的代码。
Iterator<Shape> iterator = this.myList.iterator();
while (iterator.hasNext()) {
Shape nextShape = iterator.next();
nextShape.move();
if(condition) {
iterator.remove()
}
}
onDraw 方法使用 for each 循环来绘制每个项目。尽管仅通过迭代器修改列表,但我在 onDraw 方法中得到并发修改。我尝试过 CopyOnWriteArrayList 和 Collections.synchronized,结果相同。
任何帮助将不胜感激。
【问题讨论】:
标签:
android
concurrency
android-canvas
concurrentmodification
【解决方案1】:
有几种方法可以解决这个问题。一种是在线程和 onDraw 中使用同步块,但这会阻止第二个线程与 UI 线程同时运行。
我认为最好的方法是使用两个集合。你的代码应该是这样的
onDraw(){
synchronized(myList){
//Do your draw code. Get the iterator inside this block
}
}
threadFunction(){
List threadList;
while(1){
//Update threadList, which holds all the objects
synchronized(myList){
myList = threadList.copy(); //Do a deep copy, don't just copy over the reference
}
postInvalidate();
}
}
这将使您的绘图函数对迭代结束时生成的列表副本进行操作。如果您正在处理深层副本,则不会遇到任何同步问题,因为线程不会更改同一个列表。同步块将阻止线程在绘制期间覆盖绘制函数的副本。唯一剩下的问题是线程中列表的覆盖将挂起,直到绘制完成,但由于更新频率高于它不会显示在屏幕上,我猜这是可以接受的。