Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes ConcurrentModificationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ConcurrentModificationException" Watch "ConcurrentModificationException" New topic
Author

ConcurrentModificationException

Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17
Hi,
Im getting this error when i run my program, Its running 4 instances of the same class, the code where the error is comign from is:
(tillQueue is a Queue of type person ( Queue<Person> tillQueue = new LinkedList<Person>(); )


I tried making the function synchronized but im getting the same error. Does anyone know how to fix this?
ALSO im getting some nullpointer exceptions on the for-each loop. Why is that happening, surely the loop just cycles through the tillQueue?

Thanks,
Ben
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It would be interesting to look at the stack traces for those exceptions, to see at least what line of code was throwing them.
Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17




Line 61 corresponds to: for( Person o: tillQueue)


line 66 corresponds to: switch(o.getType())
Thanks,
Ben
Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17
I threw in a dialog box, which comes up when the error occurs so i could see exactly what the program is doing when i run it. When the error occurs, the JPanel stops updating with the drawing of the images untill i resizes the JFrame. So im guessing its something to do with the drawImage and the for each loop with the person....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. The first one says "Somebody else is changing the list while this code is reading it" and the second one says "You got a null value out of the list".

So you must have more than one thread working on that list, because it's clear that the code you posted doesn't modify the list. And something is putting null values into the list, whereas your posted code is assuming it's only going to get references to actual Person objects and not nulls.

The documentation for Queue says
Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.


so you should hunt down the code which is putting nulls into the queue and fix it so it doesn't do that. Also: you chose to describe your list as a Queue. The usual way to handle a Queue is to have two processes, a producer and a consumer. The producer does nothing but add entries at the end of the queue, and the consumer does nothing but pull entries off the beginning of the queue. If you really meant to have a Queue, perhaps your should restructure your code to work that way.
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
There is a discussion on the occurrence of concurrentmodifcationexception at How To Avoid ConcurrentModificationException.
To simulate that discussion, you can force the error to occur by adding to "o" inside the loop.
This may be what is occurring via another thread.

For further debugging, you could modify the "add" function in a class derived from linkedlist to print the time/thread name of the addition. Then, also print the time / thread name at the start and end of your loop. Or, if you only add to "o" in a small number of locations, just print the time/thread at each add.

Some potential trivial fixes include
1) In drawPerson, copy the list to an array inside a sychronized block, sychronized on "o", and then iterate over the array instead of iterating over the list.
2) or, during creation of "o" synchronize the list via see linkedList.
3) You could live with the error, just think what you want to do after catching the exception in the code inside the drawPerson function. Possibly you recall the member function from the catch block, a finite number of times to prevent a race/trap condition, and if the finite number of times is exceeded, just give up or throw an exception... I don't think this is a good recommendation, but may lead you to think of an appropriate action to take when the exception is caught.
4) Put the loop inside a block that is synchronized on "o".

You might investigate further about why synchronizing the function did not correct the error, printing time/thread when drawPerson is called, along with printing modifications to "o", with/without synchronized function.

Good luck
John
Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17
Cheers Guys Solved it.
Heres the code incase someone comes accross a similar error in the future:


i also synchronized tillQueue

-Ben
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

That's not solving it, that's ignoring the symptoms. You still don't know what caused the problem or how you can properly solve it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ConcurrentModificationException
 
Similar Threads
Need help on print , scrollbar for my applet
Do while error, I'm going crazy
Drawing text inside a Graphics2D shape
Rotating a JWindow with multiple JPanels
Code to print , rotate, zoom an image from an applet