获得并发修改例外。问题出在哪儿? (码)

时间:2013-01-08 15:08:48

标签: java swing concurrentmodification

我正在用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的意思,但我不知道这里的问题在哪里。也许我应该做一些同步。如果是,请显示

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

paintComponent中迭代光束之前,创建集合的副本并使用副本进行迭代。

答案 2 :(得分:0)

您正在修改线程beams中的arraylist BeamMoveRunnable(线程,它包装此Runnable对象),并在方法paintComponent中对其进行迭代。这是异常的原因。