Concurrency Problem
Hi, i have a problem and while i think i do know what it is i cannot work out how to solve it, i am making a Breakout style game just to learn graphics capabilities of SDL and OpenGL in java. I think the problem is in the line, 'block current = (block) it.next();', but cannot see how, the error i get is:
exception at thread main, java.util.ConcurrentModException
at java.util.LinkedList$ListItr.checkForComodifications(unknown source)
'' '' '' .next(unknown source)
at breakout.update(209) --line before the line i thinks the problem
Any help would be apprieciated
//check to see if it collides with our blocks
//iteration through the list
synchronized (ListHandler.Blocks)
{
ListIterator it = ListHandler.Blocks.listIterator(0);
int count = 0;
// Go through the list
while( it.hasNext() )
{
// Retrieve the next object in the list
block current = (block) it.next();
// Draw it to the screen
boolean hit = current.check_ball_collision(our_ball);
if(hit)
{
ListHandler.Blocks.remove(count);
}
count++;
}
}
JB :WAVE:
[1321 byte] By [
arophous] at [2007-11-11 10:09:06]

# 2 Re: Concurrency Problem
I don't really know Java but there were some things I came up with that might be wrong, but what I think is really the problem might be that you are removing nodes from a list that may have more than one children. Why not simply go through the list of Blocks showing the data and then at the end try to remove them?
The only other way I came up with was a bit messy: instead of going from (0) to the end removing nodes as you go down, start from the end to (0) and remove nodes as you go up:
Ex: This means that "ListIterator it = ListHandler.Blocks.listIterator(last_node);"
Algorithm:
1. initialize array hit_nodes with length of ListHandler.Blocks //let's say 7
2. initialize variable len_nodes to length of array hit_nodes or ListHandler.Blocks
3. loop through ListHandler.Blocks reading data from it and moving to hit_nodes
4. remove node when finished with it
Pseudo-code:
for int len_nodes=(ListHandler.Blocks.length)
{
loop until len_nodes==0;
{
hit_nodes[len_nodes] = ListHandler.Blocks.listIterator(len_nodes); //move data
ListHandler.Blocks.remove(len_nodes); //now remove node
len_nodes--; //to reflect new length
}
}
Lastly, go back through from beginning of array showing the data as you go down.