gpt4 book ai didi

java - ConcurrentModificationException 但没有从 ArrayList 中删除任何内容

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

所以我正在制作一个游戏,每 5 秒必须产生一个敌人,知道当我运行我的应用程序时它会抛出异常“ConcurrentModificationException”。这是我的代码的一部分:

private void tick() {

exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
enemyY = enemyYRand.nextInt(6);
enemy.add(new Enemy(enemyY, 10, enemyImg));
}
}, 0, 5, TimeUnit.SECONDS);

if (player.y > getHeight() - playerImg.getHeight(null)) {
player.setY(-10);
}
if (player.y < 0) {
player.setY(+10);
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Enemy enemys : enemy) {
enemys.render(g);
}
player.render(g);
}

首先调用tick方法get,然后调用paintComponent方法。

这是输出:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at _47b3n.spaceinvaders.framework.Main.paintComponent(Main.java:141)
at javax.swing.JComponent.paint(JComponent.java:1053)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5223)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
at javax.swing.JComponent._paintImmediately(JComponent.java:5171)
at javax.swing.JComponent.paintImmediately(JComponent.java:4982)
at javax.swing.RepaintManager$4.run(RepaintManager.java:824)
at javax.swing.RepaintManager$4.run(RepaintManager.java:807)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
at javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

看来您有 2 个线程,每个线程同时对 ArrayList 进行操作。

其中之一是在事件调度线程中,其中 paintComponent 被调用。您正在使用增强的 for 循环,它在内部使用 Iterator。该迭代器引发了 ConcurrentModificationException

当您调用 enemy.add(new Enemy(enemyY, 10, heavenlyImg) 时,您正在其他线程中的 tick 方法中修改 ArrayList ));

Iterator 注意到 ArrayList 被修改并抛出异常。当您显示敌人时,您的 ArrayList 必须具有一致的 View 。

通过将对此 ArrayList 的读写操作封装在 中,在某个对象(甚至可能是 enemy 本身)上同步对此 ArrayList 的访问>同步 block 。这意味着只能同时执行这些 block 之一。

synchronized (enemy) {
enemy.add(new Enemy(enemyY, 10, enemyImg));
}

synchronized (enemy) {
for(Enemy enemys : enemy) {
enemys.render(g);
}
}

关于java - ConcurrentModificationException 但没有从 ArrayList 中删除任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35070495/

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