ConcurrentModificationException Minigame

时间:2013-07-19 07:53:11

标签: java exception java.util.concurrent concurrentmodification

你好。

我正在运行服务器并添加了迷你游戏。

每当游戏即将开始时......首先调用onStart()。现在我在这一行得到了ConcurrentModificationException:

for(Player p : waiting) {

以下是方法:

    public void onStart() {
    trawler.players.clear();
    for(Player p : waiting) {
        if(!boat.playerInArea(p)) {
            waiting.remove(p);
        }
    }

    for(Player p : waiting) {
        trawler.players.add(p);
    }

    trawler.start();
    waiting.clear();
}

如果您需要课程,请点击这里:

TrawlerWaitingRoom.java:

package server.model.minigames.trawler;

import server.model.players.Location;
import server.model.players.Player;

public class TrawlerWaitingRoom extends WaitingRoom {
private Trawler trawler;
//private Location boat = new Location(2668,2674,3165,3185);
private Location boat = new Location(2808, 2811,3415,3425);


public TrawlerWaitingRoom(Trawler trawler) {
    super(1, 2);
    this.trawler = trawler;
}

@Override
public boolean startGame() {
    if(trawler.inProgress()) {
        return false;
    }
    return true;
}

@Override
public void onStart() {
    trawler.players.clear();
    for(Player p : waiting) {
        if(!boat.playerInArea(p)) {
            waiting.remove(p);
        }
    }

    for(Player p : waiting) {
        trawler.players.add(p);
    }

    trawler.start();
    waiting.clear();
}

@Override
public void onLeave(Player p) {
    p.asClient().getPA().movePlayer(2804, 3421, 0);
    //p.asClient().getPA().movePlayer(2676, 3170, 0);
}

@Override
public void onJoin(Player p) {
    p.asClient().getPA().movePlayer(2808, 3421, 1);
    //p.asClient().getPA().movePlayer(2672, 3170, 1);
    if(!isActive()) {
        p.asClient().sendMessage(trawler.getGameTime() == 0 ? "The trawler will be returning in less than a minute!" : "The trawler will return in "+trawler.getGameTime() + (trawler.getGameTime() == 1 ? " minute" : " minutes")+"!");
    } else {
        p.asClient().sendMessage(getTimeRemaining() == 0 ? "The trawler will be leaving in less than a minute!" : "The trawler will leave in "+ getTimeRemaining() + (getTimeRemaining() == 1 ? " minute" : " minutes")+"!");
    }
}

@Override
public Location getLocation() {
    return boat;
}

@Override
public void onTimeChange() {
    for(Player p : waiting) {
        if(!isActive()) {
            p.asClient().sendMessage(trawler.getGameTime() == 0 ? "The trawler will be returning in less than a minute!" : "The trawler will return in "+trawler.getGameTime() + (trawler.getGameTime() == 1 ? " minute" : " minutes")+"!");
        } else {
            p.asClient().sendMessage(getTimeRemaining() == 0 ? "The trawler will be leaving in less than a minute!" : "The trawler will leave in "+ getTimeRemaining() + (getTimeRemaining() == 1 ? " minute" : " minutes")+"!");
        }
    }
}

@Override
public boolean canStart() {
    if(trawler.inProgress()) {
        return false;
    }
    if(waiting.size() < minimumPlayers) {
        return false;
    }
    return true;
}

}

GroupMinigame.java:

package server.model.minigames.trawler;

import server.model.minigames.trawler.WaitingRoom;

public abstract class GroupMinigame {

public abstract WaitingRoom getWaitingRoom();

public abstract String getWaitingRoomMessage();
}

WaitingRoom.java: http://pastebin.com/KkC8ReWV

Trawler.java: http://pastebin.com/XW5XrsjR

提前致谢!

5 个答案:

答案 0 :(得分:6)

问题在于:

for(Player p : waiting) {
    if(!boat.playerInArea(p)) {
        waiting.remove(p); // BOOM
    }
}

您无法直接修改您正在迭代的收藏品。

相反,您必须使用Iterator

for(Iterator<Player> i = waiting.iterator(); i.hasNext();) {
    Player p = i.next(); 
    if(!boat.playerInArea(p)) {
        i.remove(); // Allowed with an iterator
    }
}

答案 1 :(得分:0)

您正在使用for-each循环在对象上创建迭代器。您正在尝试编辑它,这在Java中是不允许的。

for(Player p : waiting) {
    // Iterator created.
    if(!boat.playerInArea(p)) {
        // Change to iterated object.
        waiting.remove(p);
    }
}

您可以选择创建新列表,并覆盖旧列表。例如:

private List<Waiting> newWaiting = new ArrayList<Waiting>();

for(Player p : waiting) {
   // Iterator created on the WAITING object.
   if(boat.playerInArea(p)) {
       newWaiting.add(p);
       // Logic is flipped, so those that pass are carried forward.
   }
}
waiting = newWaiting;
// Set waiting's reference to point to newWaiting's reference. 

答案 2 :(得分:0)

在迭代List时,使用Iterator#remove()List删除元素。

Iterator<Player> itr = waiting.iterator();
while(itr.hasNext()) {
    Player p = itr.next(); 
    if(!boat.playerInArea(p)) {
      itr.remove(); 
    }
}

答案 3 :(得分:0)

这很简单:当您将对象用作循环时,您将从列表waiting中删除对象。这是不允许的。 请尝试以下方法:

List<Player> temporaryList = new LinkedList<Player>(waiting);
for(Player p : temporaryList ) {
    if(!boat.playerInArea(p)) {
        waiting.remove(p);
    }
}

答案 4 :(得分:0)

在纠正和理解此错误之前,您需要遵循两个逻辑步骤。

  1. How does the Java 'for each' loop work?(提示:迭代器)

  2. 如何使用迭代器导致Concurrent Modification exception