gpt4 book ai didi

java - 不知道为什么会出现并发修改异常

转载 作者:行者123 更新时间:2023-12-01 10:30:48 25 4
gpt4 key购买 nike

我正在迭代两个 Orb 对象列表

目前,当我浏览这两个列表时,我会在迭代它们时添加和删除 Orb 对象。

//first I iterate through the "friendly" orb list.
for(orbIterator = friendlyOrbs.listIterator(); orbIterator.hasNext();) {
//I have only included what I (think) is the relevant code.

Orb tempOrb = orbIterator.next(); //Set a reference to the current orb object so I can update it and use its properties.

//Some drawing stuff I don't include.

//The orb is not dead
int orbResult = tempOrb.Update(); //Updates the orb object and returns its current state.

if(orbResult == 0) {

} else if(orbResult == 2) {
//Died a natural death
//Stuff to do when the orb dies naturally.
orbIterator.remove();
} else if(orbResult == 3) {
//Killed by player.
//stuff to do when player kills orb.
orbIterator.remove();
orbIterator.add(new Orb(...));
}
}

现在这就是我迭代“敌人”球体列表的方式:

   for(orbIterator = enemyOrbs.listIterator(); orbIterator.hasNext();) {
Orb tempOrb = orbIterator.next();

int orbResult = tempOrb.Update();

if(orbResult == 0) {

} else if (orbResult == 2) {
//Enemy orb died a natural death.
orbIterator.remove();

enemyOrbs.add(...);
} else if(orbResult == 3) {
//Enemy orb killed by player.
orbIterator.remove();
} else {
//Draw the orb.
}
}

我还有两个计时器任务(一个用于生成友好的球体,一个用于生成敌人的球体),它们设置一个 boolean 标志,告诉 onDraw 方法是否应该向屏幕添加一个新的球体。

//the friendly orb timertask

public static class FriendlyOrbTimerTask extends TimerTask implements Cloneable {

@Override
public void run() {
if(SystemClock.uptimeMillis() - drawCallTime < 100) {
Log.i("ADDING NEW ORB", "ADDING NEW ORB");
friendlyOrbToBeAdded = true;
friendlyOrbSpawnInterval += 250;
TIMER.schedule(this.clone(), friendlyOrbSpawnInterval);
} else {
pauseStartTime = SystemClock.uptimeMillis();
}

}

@Override
public FriendlyOrbTimerTask clone(){
return new FriendlyOrbTimerTask(); //add parameters from the current contextif needed.
}
}

这是敌人Orb计时器任务:

public static class EnemyOrbTimerTask extends TimerTask implements Cloneable {



@Override
public void run() {

if(SystemClock.uptimeMillis() - drawCallTime < 50) {
enemyOrbToBeAdded = true;
enemyOrbSpawnInterval+= 250;
TIMER.schedule(this.clone(), enemyOrbSpawnInterval);
}
}

@Override
public EnemyOrbTimerTask clone(){
return new EnemyOrbTimerTask(); //add parameters from the current contextif needed.
}
}

以下 if 语句告诉我是否应该向屏幕添加一个新的球体(位于 onDraw 中)

   //These are located right before I iterate through the lists.
if(friendlyOrbToBeAdded) {
friendlyOrbs.add(...);
friendlyOrbToBeAdded = false;
}
if(enemyOrbToBeAdded) {
enemyOrbs.add(...);
enemyOrbToBeAdded = false;
}

问题是有时我的应用程序会随机崩溃:

  Orb tempOrb = orbIterator.next(); //This is in the enemyOrb iterator loop.

它给出了异常:java.util.ConcurrentModificationException

我不知道为什么会发生这种情况,因为我正在使用 listIterator.add()listIterator.remove() 这应该允许我避免这些异常。

最佳答案

如果你的复制粘贴是准确的,那么我认为这就是问题所在:

   for(orbIterator = enemyOrbs.listIterator(); orbIterator.hasNext();) {
...
enemyOrbs.add(...);
...
}

您在迭代集合时调用了add

关于java - 不知道为什么会出现并发修改异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com