[Solved, thanks guys]Concurrent Modification Exception

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 10111 Views - Last Post: 25 November 2009 - 12:25 PM Rate Topic: -----

#1 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

[Solved, thanks guys]Concurrent Modification Exception

Post icon  Posted 19 November 2009 - 12:34 AM

So I was getting the ConcurrentModificationException error, due to attempting to access an ArrayList and write to it at the same time. After looking into the problem more and playing with it a bit I found out that, at least for now, it appears that simply using a try catch and basically ignoring it allows the program to run just fine.


So my question is: Although I should be sterilizing my array to stop this from accruing, is it okay to allow it to continue through a try catch? Keeping in mind that it runs perfectly with the try catch.



Because this is not directly pertaining to actual code I haven't supplied mine, but if you would like to see I'm more then happy to oblige.

This post has been edited by virgul: 19 November 2009 - 09:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved, thanks guys]Concurrent Modification Exception

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 01:08 AM

that will compromise your data integrity. In some cases if it is being accessed concurrently the outcome can be unpredictable, which is why the exception is thrown in the first place. You do not usually want to just wrap those sorts of exceptions.
Was This Post Helpful? 0
  • +
  • -

#3 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 01:48 AM

As far as I can tell the data stays completely intact, but I may be wrong... In which case should I be sterilizing my ArrayList?

So, I actually have never done this before and need a bit of help. How would I go about do this?
Was This Post Helpful? 0
  • +
  • -

#4 anirelles  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 01-January 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 02:22 AM

Try to use a Vector instead of ArrayList, Vector is thread safe. Maybe it will work I don't know I didn't try.

Other option. Try to synchronize the critical section where you access the arraylist and where you read it. I didn't try either.

Last option. Before accessing the ArrayList clone it and access the clone. I know it works.
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,521
  • Joined: 20-September 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 02:44 AM

Firstly, you need to determine why you got the exception. I suspect it's nothing to do with multithreading. Can you post your code?
Was This Post Helpful? 0
  • +
  • -

#6 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 10:38 AM

public void paintComponent(Graphics p) {
		super.paintComponent(p);
		if (paintInProgress)
			return;
		paintInProgress = true;
		Graphics2D g2d;
		g2d = (Graphics2D) p;
		paintMap(p);
		drawEnemy(p);//error is here
		drawPerson(g2d);
		paintInProgress = false;
	}



public void drawEnemy(Graphics p) {
		try {
			for (Monster monster : OnScreenCollisionList) {//error is on this line, pointing to the above error
							// excessive amounts of code left out
							// this basically finds the proper image for the monster that is in the list
							// then it paints it to the proper location
						}
					   } catch (java.util.ConcurrentModificationException e) {
					   }//ignore...



This is where the compiler is telling me the problem is, I put it in this order because this is the order it is in my code. If you want any explanation of what is going on in code I'll try my best to explain without going to far into the actual code...
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,521
  • Joined: 20-September 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 12:18 PM

Quote

// this basically finds the proper image for the monster that is in the list


How? Are removals from the List involved?
Was This Post Helpful? 0
  • +
  • -

#8 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 05:11 PM

try {
	for (Monster monster : OnScreenCollisionList) {
		int xLocation = w * (monster.getGridedX() - Char.getGridedX() + 3) + (int) tileXoffset
				- (int) monster.getMonsterTileXoffset();
		int yLocation = h * (monster.getGridedY() - Char.getGridedY() + 3) + (int) tileYoffset
				- (int) monster.getMonsterTileYoffset();
		if (monster.getType().equals("Skeleton")) {
			p.drawImage(monster.Skeleton, xLocation, yLocation, monster.Skeleton.getWidth(this) * 2,
					monster.Skeleton.getHeight(this) * 2, this);
			monster.setWidth(monster.Skeleton.getWidth(this) * 2);
			monster.setHeight(monster.Skeleton.getHeight(this) * 2);
		}

//..Removed code that does same as above to save space..\\

		// look into the use of ovals instead of rectangles... maybe a very good solution
				// only problem is that Oval test = new Oval(); does not exist.......
		p.drawOval(xLocation, yLocation, monster.getWidth(), monster.getHeight());

		monster.setMonsterAreaRect(new Rectangle(xLocation, yLocation, monster.getWidth(), monster
				.getHeight()));
		if (isPlayerColliding(monster))
			p.drawString("Monster is in Player area", 100, 100);

	}
} catch (java.util.ConcurrentModificationException e) {
	// System.out.println("Try catch thinngy");
}



In this area, as you can see its just referencing it. I looked into what you said and realized that even through the problem isn't pointing here it may be caused by this area of code

for (Monster monster : monsterList) {
	if (monster.onScreen(Char, map)) {// if monster is on screen
		if (!monster.isCounted()) {// on screen and not counted
			monster.setCounted(true);// now is counted
			OnScreenCollisionList.add(monster);
		}
		// move the enemy because its on screen
		moveEnemy(monster, numOfMonstersOnScreen);
	}

	// monster isn't on screen, but is still counted on screen, lets fix that
	if (!monster.onScreen(Char, map) && monster.isCounted()) {
		OnScreenCollisionList.remove(monster);
		monster.setCounted(false);
	}
	numOfMonstersOnScreen = OnScreenCollisionList.size();
}



Could the problem be that im removing a monster from the list before the entire list gets iterated through?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 07:03 PM

Just use a Vector instead
ArrayList and Vector are almost the same (functionnality and method names) but.... Vector access are synchronized so safe to use in mutithreads environment
Was This Post Helpful? 0
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 08:09 PM

Just attempted using a vector, it appears that I still have the problem. Should I put the accessing and adding/removing onto a different thread for the vector to work properly?

Just a note here is the full error message, encase that makes a difference
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
	at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
	at java.util.AbstractList$Itr.next(Unknown Source)
	at game.Play.drawEnemy(Play.java:270)
	at game.Play.paintComponent(Play.java:232)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


Play.java:270 is my for each loop area posted above and
Play.java:232 is my paint component

This post has been edited by virgul: 19 November 2009 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 08:26 PM

Now I see your problem :)

We should always require code at the first post.

for (Monster monster : OnScreenCollisionList)

you cannot use AbstarctList for() loop for what you are doing. Nothing really related to thread
Even this single thread process would crash as you crash

Vector<String> vector;

for(String x  :  vector) {
   ....
   if(condition) {
	vector.add("Hello");   // <-- this will make you crash
}


You cannot do that because inserting/removing an element into the Collection screws the AbstractList generated by the Iterator
When you use the for(Object : Collection) syntax you cannot modify the original Collection while you process the AbstractList
I admit that is not well documented but modifying a Collection while you use an Iterator to scan it is NOT supported thow your Exception

However the good old for() syntax is safe and would work in your case

for(int i = 0; i < vector.size(); i++) {
	String str = vector.get(i);
....
}


This post has been edited by pbl: 19 November 2009 - 09:15 PM

Was This Post Helpful? 1
  • +
  • -

#12 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 19 November 2009 - 09:10 PM

Thank you so much!

Appears that I should definitely watch when I use my for(each) loops.

Thank You everyone
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,521
  • Joined: 20-September 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 20 November 2009 - 01:20 AM

You can add elements when you iterate a List, but you must use a proper ListIterator rather than an enhanced for loop, or indeed any kind of loop:

http://java.sun.com/...tor.html#add(E)
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 20 November 2009 - 04:08 PM

View Postg00se, on 20 Nov, 2009 - 12:20 AM, said:

You can add elements when you iterate a List, but you must use a proper ListIterator rather than an enhanced for loop, or indeed any kind of loop:

http://java.sun.com/...tor.html#add(E)

Thanks for clarifying :P
Funny it adds just before the cursor... what if your Collection is sorted ?
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,521
  • Joined: 20-September 08

Re: [Solved, thanks guys]Concurrent Modification Exception

Posted 20 November 2009 - 04:25 PM

Quote

what if your Collection is sorted ?


Not sure - let me know if you try it ;-)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2