Iterator.next()上的游戏循环上的ConcurrentModificationException

时间:2015-03-04 22:11:21

标签: java android

我正在努力将此问题排序在我运行的游戏循环中。

我在Update()过程中有以下一个帖子:



ListIterator<ExplosionClass> litr = Explosions.listIterator();
			while(litr.hasNext()){
		        ExplosionClass element = (ExplosionClass) litr.next();
				if (element.FinishedExplosion) {
					litr.remove();
				}
			}
&#13;
&#13;
&#13;

我在litr.next();

上间歇性地得到一个ConcurrentModificationException

我正在通过另一个程序不时向Explosions列表中添加新项目。

第二个帖子的My Game循环如下:

&#13;
&#13;
public  void run() {
		Canvas canvas;
		Log.d(TAG, "Starting game loop");

		long beginTime;		// the time when the cycle begun
		long timeDiff;		// the time it took for the cycle to execute
		int sleepTime;		// ms to sleep (<0 if we're behind)
		int framesSkipped;	// number of frames being skipped 

		sleepTime = 0;
		
		// render state to the screen
		// draws the canvas on the panel
		
		while (running) {
		//while (!paused && running) {
			canvas = null;
			synchronized (this) {
                while (isPaused) {
                    try {
                        try {
            				canvas = this.surfaceHolder.lockCanvas();
                            synchronized (surfaceHolder) {
            					this.gamePanel.render(canvas);				
                            }
                            } finally {
                                if (canvas != null) {
                					surfaceHolder.unlockCanvasAndPost(canvas);
                                }
                        }
                        
                        this.wait();

                    } catch (InterruptedException e) {
                    }
                }
            }
			// try locking the canvas for exclusive pixel editing
			// in the surface
			try {
				canvas = this.surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) {
					beginTime = System.currentTimeMillis();
					framesSkipped = 0;	// resetting the frames skipped
					// update game state 
					if (!isPaused && running) this.gamePanel.update();
                    
					// render state to the screen
					// draws the canvas on the panel
					if (!isPaused) this.gamePanel.render(canvas);				
					// calculate how long did the cycle take
					timeDiff = System.currentTimeMillis() - beginTime;
					// calculate sleep time
					sleepTime = (int)(FRAME_PERIOD - timeDiff);
					
					if (sleepTime > 0) {
						// if sleepTime > 0 we're OK
						try {
							// send the thread to sleep for a short period
							// very useful for battery saving
							Thread.sleep(sleepTime);	
						} catch (InterruptedException e) {}
					}
					
					while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
						// we need to catch up
						// update without rendering
						if (!isPaused && running) this.gamePanel.update(); 
						// add frame period to check if in next frame
						sleepTime += FRAME_PERIOD;	
						framesSkipped++;
					}
				}
			} finally {
				// in case of an exception the surface is not left in 
				// an inconsistent state
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}	// end finally
		} 
		
		try {
			Thread.sleep(FRAME_PERIOD);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	//}  
	} 
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

实际上,你不能这样做:你必须确保其他线程在使用迭代器迭代时使用synchronized或其他锁定机制来更改列表。

答案 1 :(得分:0)

问题是,如果两个迭代器同时修改Collection的内部结构会导致ConcurrentModification异常。要处理此问题,请使用同步集合(CopyOnWriteArrayList)

答案 2 :(得分:0)

当您在列表上调用迭代器时,在迭代器正在工作时无法修改它。

相关问题