TryCatch ConcurrentModificationException捕获`30%的时间

Cab*_*ose 2 java swing try-catch concurrentmodification

我正在使用迭代器从列表中移除一个射弹,如果它超出了我的JPanel的边界.在使用迭代器之前它不会工作,但是只要我将方法放入try-catch中,迭代器就可以工作ConcurrentModificationException.代码现在可以工作,并且成功地从列表中移除了射弹,但是大约30%的时间,捕获命中并导致我的程序中出现断断续续的情况.我不知道为什么它只是偶尔捕获,但在有限的时间内,我的教授能够看到它,他认为这可能是一个同步问题.

这是运行我的程序的循环:

private void executeGameLoop() 
        {

            long nextFrameStart = System.nanoTime();
            while(panel.getRunning()) 
            {
                do 
                {
                    panel.repaint();
                    nextFrameStart += FRAME_PERIOD;
                } while(nextFrameStart < System.nanoTime());

                long remaining = nextFrameStart - System.nanoTime();
                panel.update();

                if (remaining > 0) 
                {
                    try 
                    {
                        Thread.sleep(remaining / 1000000);
                    } 
                    catch(Throwable e) 
                    {
                        System.out.println(e.getMessage()); 
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

这由循环at调用panel.update.它现在处理弹丸的更新:

public void update()
        {       
            randX = (int)((Math.random() * getWidth()) - (int)(Math.random() * getWidth()));
            randY = (int)((Math.random() * getHeight()) - (int)(Math.random() * getHeight()));

            int sizeX;
            int sizeY;

            try
            {
                Iterator<Projectile> it = shots.iterator();
                for(Projectile a : shots)
                {
                    if(!a.equals(null))
                    {   
                        sizeX = a.getDisplayX();
                        sizeY = a.getDisplayX();

                        if((!checkCoords((int)a.getX(), (int)a.getY(), sizeX, sizeY)) && a.hasTarget())
                        {
                            a = null;
                            if(it.next().equals(null));
                                it.remove();
                        }

                        else if(a.hasTarget())
                        {
                            a.update();
                        }
                    }   
                }
            }
            catch (ConcurrentModificationException e){ System.out.println(e.getMessage() + " Catch"); } 
        }
Run Code Online (Sandbox Code Playgroud)

最后两种方法是我创建弹丸的机制:

private void createProjectile(int x, int y)
{
    total++;
    if(shots.size() < shotCount)
    {
        Projectile temp = new Projectile(randX, randY);
        temp.setTarget((x + temp.getSprite().getDisplayImg().getWidth() / 8),
                        (y - temp.getSprite().getDisplayImg().getHeight() / 8));
        temp.setHasTarget(true);
        shots.add(temp);
        msg("Target: (" + x + ", " + y + ")");
    }
}

@Override
public void mouseClicked(MouseEvent e) 
{
    createProjectile(e.getX(), e.getY());
}
Run Code Online (Sandbox Code Playgroud)

任何关于为什么会发生这种情况或如何纠正它的见解将不胜感激.

chr*_*ke- 6

Iteratorfor循环中有一个开放(加上额外的Iterator it),你正在添加值createProjectile.您需要synchronized打开两个块shots,或者(我的建议)制作一份副本shots来执行您的绘图:

List<Projectile> shotsToPaint;
synchronized(shots) { shotsToPaint = [`ImmutableList`][1].copyOf(shots); }
Run Code Online (Sandbox Code Playgroud)

并在中应用适当的同步createProjectile.根据性能敏感程度,您可以同步整个方法shots或同步shots,检查大小,Projectile在非同步块中创建新方法,然后同步以重新检查列表大小并添加.