本文介绍了2010年java并发修改异常崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绘制一些存储在 ArrayList 中的粒子.此代码工作正常:

super.paintComponent(g);对于(粒子b:粒子Arr){g.setColor(b.getColor());g.fillOval(b.getXCoor() + 5,b.getYCoor(),b.getParticleSize(),b.getParticleSize());}

但是这段代码会抛出一个并发修改异常:

公共无效paintComponent(图形g){//绘制粒子super.paintComponent(g);对于(粒子b:粒子Arr){g.setColor(b.getColor());如果 (b.isDead())粒子Arr.remove(b);否则如果 (!b.isVanishing())g.fillOval(b.getXCoor(),b.getYCoor(),b.getParticleSize(),b.getParticleSize());别的 {g.fillOval(b.getXCoor() + 5,b.getYCoor(),b.getParticleSize(),b.getParticleSize());g.fillOval(b.getXCoor() - 5,b.getYCoor(),b.getParticleSize(),b.getParticleSize());g.fillOval(b.getXCoor(),b.getYCoor() + 5,b.getParticleSize(),b.getParticleSize());g.fillOval(b.getXCoor(),b.getYCoor() - 5,b.getParticleSize(),b.getParticleSize());}}

我糊涂了.这是迭代器的乱码,运行缓慢.itr =particleArr.iterator();

 super.paintComponent(g);而 (itr.hasNext()){粒子=itr.next();g.setColor(particle.getColor());如果(粒子.isDead())itr.remove();否则如果(粒子.isVanishing())g.fillOval(particle.getXCoor(),particle.getYCoor(),粒子.getParticleSize(),particle.getParticleSize());别的 {g.fillOval(particle.getXCoor() + 5,particle.getYCoor(),粒子.getParticleSize(),particle.getParticleSize());g.fillOval(particle.getXCoor() - 5,particle.getYCoor(),粒子.getParticleSize(),particle.getParticleSize());g.fillOval(particle.getXCoor(),particle.getYCoor() + 5,粒子.getParticleSize(),particle.getParticleSize());g.fillOval(particle.getXCoor(),particle.getYCoor() - 5,粒子.getParticleSize(),particle.getParticleSize());}

解决方案

尝试从数组列表中获取一个迭代器,然后调用迭代器上的 remove() 方法来删​​除项目.

示例

Iterator itr =particleArr.iterator();而(itr.hasNext()){粒子 b = (粒子)itr.next();如果 (b.isDead())itr.remove();}

只是使示例与您的代码更相关.

Painting some particles stored in an ArrayList. This code works fine:


 super.paintComponent(g);
             for (Particle b: particleArr){
                  g.setColor(b.getColor());
                  g.fillOval(b.getXCoor() + 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
             }

However this code throws a concurrent modification exception:


        public void paintComponent(Graphics g){
            //paint particles
             super.paintComponent(g);
             for (Particle b: particleArr){
                  g.setColor(b.getColor());
                  if (b.isDead())
                      particleArr.remove(b);
                  else if (!b.isVanishing())
                      g.fillOval(b.getXCoor(),b.getYCoor(),
                            b.getParticleSize(),b.getParticleSize());
                  else {
                      g.fillOval(b.getXCoor() + 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor() - 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor(),b.getYCoor() + 5,
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor(),b.getYCoor() - 5,
                             b.getParticleSize(),b.getParticleSize());
                  }
             }

I confused.This is the garbled code with the iterator, it is running slow.


            itr = particleArr.iterator();

         super.paintComponent(g);
         while (itr.hasNext()){
             particle=itr.next();
              g.setColor(particle.getColor());
              if (particle.isDead())
                  itr.remove();
              else if (particle.isVanishing())
                  g.fillOval(particle.getXCoor(),particle.getYCoor(),
                        particle.getParticleSize(),particle.getParticleSize());
              else {
                  g.fillOval(particle.getXCoor() + 5,particle.getYCoor(),
                         particle.getParticleSize(),particle.getParticleSize());
                  g.fillOval(particle.getXCoor() - 5,particle.getYCoor(),
                         particle.getParticleSize(),particle.getParticleSize());
                  g.fillOval(particle.getXCoor(),particle.getYCoor() + 5,
                         particle.getParticleSize(),particle.getParticleSize());
                  g.fillOval(particle.getXCoor(),particle.getYCoor() - 5,
                         particle.getParticleSize(),particle.getParticleSize());
              }

解决方案

Try getting an Iterator from the array list then calling the remove() method on the iterator to remove the item.

Example

Iterator itr = particleArr.iterator();
while(itr.hasNext()) {
   Particle b = (Particle)itr.next();
   if (b.isDead())
      itr.remove();
}

Edit: Just made the example a bit more relevant to your code.

这篇关于2010年java并发修改异常崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:08