简体   繁体   English

不知道为什么会有并发修改异常

[英]Not sure why there is concurrent modification exception

I am iterating through two lists of Orb objects 我正在遍历两个Orb对象列表

Currently as I go through both lists I add and remove Orb objects as I iterate through them. 目前,当我浏览两个列表时,我在遍历它们时添加和删除了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(...));
        }
    }

Now this is how I iterate through the "enemy" orb list: 现在,这就是我遍历“敌人”球列表的方式:

   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.
        }
    }

I also have two timer tasks (one for spawning friendly orbs, one for spawning enemy orbs) that set a boolean flag that tells the onDraw method if it should add a new orb to the screen. 我还有两个计时器任务(一个用于生成友好的球,一个用于生成敌人的球),它们设置了一个布尔标志,该标志告诉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.
    }
}

Here is the enemyOrb timertask: 这是敌人的球计时器任务:

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.
    }
}

Here are the if statements that tell me if I should add a new orb to the screen (located in onDraw) 这是if语句,告诉我是否应在屏幕上添加新的球体(位于onDraw中)

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

The problem is sometimes my app randomly crashes on the line that says: 问题是有时我的应用程序随机崩溃,并显示以下内容:

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

It gives the exception: java.util.ConcurrentModificationException 它给出了异常: java.util.ConcurrentModificationException

I don't know why this is happening because I am using listIterator.add() and listIterator.remove() which should allow me to avoid these exceptions. 我不知道为什么会这样,因为我正在使用listIterator.add()listIterator.remove() ,这应该允许我避免这些异常。

If your copy-pasting is accurate, then I think this is the problem: 如果您的复制粘贴正确,那么我认为这就是问题所在:

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

You have a call to add on the collection while you are iterating it. 在迭代集合时,您可以致电add集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM