gpt4 book ai didi

java - 获取并发修改异常。哪里有问题? (代码)

转载 作者:行者123 更新时间:2023-12-02 07:21:07 25 4
gpt4 key购买 nike

我正在用 Java 编写一个简单的游戏。主要代码如下:

public class MainPanel extends JPanel {
private Player player = new Player(100, 100, 3, 3);
private Point2D targetPoint = new Point2D.Float(130, 350); //Pos on begin
private ArrayList<Beam> beams = new ArrayList<Beam>();

public MainPanel() {
setPreferredSize(new Dimension(300, 400));

addMouseMotionListener(new MouseMotionHandler());
//Add shortcuts
makeShortcut("player.BM1", "F1", new SetBeamModeAction(1));
makeShortcut("player.BM2", "F2", new SetBeamModeAction(2));

//Start threads
Thread t = new Thread(new PlayerMoveRunnable());
t.start();
Thread t2 = new Thread(new PlayerShootRunnable());
t2.start();
}

public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, 300, 400);
//Draw player
g2.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), null);
//Draw beams
for (Beam beam : beams) {
g2.drawImage(beam.getImage(), (int)beam.getX(), (int)beam.getY(), null);
}
}

//Thread running all the time
private class PlayerMoveRunnable implements Runnable {
public void run() {
try {
while (true) {
player.moveToPoint(targetPoint);
repaint();
Thread.sleep(15);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

//Thread working all the time
private class PlayerShootRunnable implements Runnable {
public void run() {
try {
while (true) {
//Choose which beam to shoot (depends on set mode)
Thread t;
switch (player.getBeamMode()) {
case 1:
t = new Thread(new BeamMoveRunnable(new Beam1(player.getX()+18, player.getY(), 0, -15)));
break;
case 2:
t = new Thread(new BeamMoveRunnable(new Beam2(player.getX()+18, player.getY(), 0, -30)));
break;
default:
t = null;
break;
}
t.start();
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private class BeamMoveRunnable implements Runnable {
private Beam beam;

public BeamMoveRunnable(Beam beam) {
this.beam = beam;
}

public void run() {
Beam beam = this.beam;
beams.add(beam);
try {
while (true) {
if (beam.getY() <= 0) {
beams.remove(beam);
break;
}
beam.move();
repaint();
Thread.sleep(20);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

[它不是完整的代码。我剪了几行,这肯定不会引起问题]

我收到这样的错误:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at spacecommander.MainPanel.paintComponent(MainPanel.java:53)
at javax.swing.JComponent.paint(JComponent.java:1054)

等等...

问题出在哪里?我知道 ConcurrentModificationException 是什么意思,但我不知道问题出在哪里..也许我应该进行一些同步。如果是,请显示在哪里

最佳答案

您正在另一个线程中修改光束列表,例如当您迭代集合时,BeamMoveRunnable

为每个对象创建一个线程非常浪费且难以管理。

我建议你有一个线程,它定期调用每个光束上的一个方法来移动。我建议您在绘制梁时不要添加或删除梁,这样您就不必同步对集合的访问。

关于java - 获取并发修改异常。哪里有问题? (代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14217975/

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