Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Removing two objects at once failure

Started by Patriarch K Jan 13, 2013 at 1:41 AM 3 replies 1.4k views
Original Post
Patriarch K
Patriarch K
I'm making a java game at the moment and when I happen to kill two enemies at the exactly same time then the game crashes. I suppose it's something with threads and serializable or something. But I don't know. I use the following code when I'm removing objects from the arrayList. The problem appears when I kill two enemies at EXACTLY the same time. Then it freezes and say:

Exception in thread "Thread-3" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at Main.checkCollisions(Main.java:168)
at Main.run(Main.java:400)
at java.lang.Thread.run(Unknown Source)



// Skott med fiende
Iterator b = bubblor.iterator();
while(b.hasNext()){
Iterator e = enemies.iterator();
Bullet bob = b.next();
while(e.hasNext()){

Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove();
e.remove();
} } }
RulerOfNothing
RulerOfNothing
From the documentation of the ArrayList:
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)
This means that you'll have to prevent two threads from accessing the ArrayList at the same time; this could be accomplished by having a wrapper of the ArrayList that queues up addition and deletion operations, or by eschewing the use of multiple threads entirely (although you may have a good reason for going threaded)
Patriarch K
Patriarch K
That must be the problem then. But I'm not very into that kind of stuff. Could you explain a little bit how to do what you mention and give some kind of code example?
Can I just do like this and NOTHING ELSE? This does not work because I get problems on the b.remove() every time when two bullets is shot at the same time and removed at the same time.

List list = Collections.synchronizedList(new ArrayList(...));

and:

synchronized(bubblor) {
Iterator b = bubblor.iterator();
while(b.hasNext()){
synchronized(enemies) {
Iterator e = enemies.iterator();
Test bob = b.next();
while(e.hasNext()){

Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove();
rip-off
rip-off

You have at least one bug that is not thread related. If you are using threads, you may have more bugs.

The problem is, imagine that a given bullet intersects two enemies in a single frame. What your code does is call remove() twice on the bullet iterator in this case.

The fix is to only remove the bullet iterator once, if any collisions have occurred:

Iterator b = bullets.iterator();
while(b.hasNext()){
Bullet bullet = b.next();
Rectangle bulletBounds = bullet.getBounds();
boolean collision = false;
Iterator e = enemies.iterator();
while(e.hasNext()){
Enemy enemy = e.next();
Rectangle enemyBounds = enemy.getBounds();
if(enemyBounds.intersects(bulletBounds) ){
collision = true;
e.remove();
}
}
if(collision) {
b.remove();
}
}

Perhaps better is to create a helper function to clarify the logic:

private static int removeIntersectingEnemies(Rectangle bounds, List enemies) {
int enemiesRemoved = 0;
Iterator iterator = enemies.iterator();
while(iterator.hasNext()){
Enemy enemy = iterator.next();
Rectangle enemyBounds = enemy.getBounds();
if(enemyBounds.intersects(bounds) ){
++enemiesRemoved;
iterator.remove();
}
}
return enemiesRemoved;
}
Iterator iterator = bullets.iterator();
while(iterator.hasNext()){
Bullet bullet = iterator.next();
int enemiesRemoved = removeIntersectingEnemies(bullet.getBounds(), enemies);
if(enemiesRemoved > 0) {
iterator.remove();
}
}
Patriarch K
Patriarch K
Thanks a lot! That worked perfectly fine for me. Problem solved :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.