I got a quick question about ArrayLists
I have 2 ArrayLists, one for active TouchPoints and one for inactive TouchPoints.
In an Event Listener, When a finger is pressed on the screen the first object in my inactive list is moved over to my active list.
public void TouchPointDown()
{
TouchPoint o = inactiveTouchPoint.get(0);
o.Revive();
activeTouchPoint.add(b);
inactiveTouchPoint.remove(b);
}
Now when a finger is removed from the screen, I do the reverse. I remove the TouchPoint from the active list and move it back over to the inactive list.
public void MakeTouchPointInactive(int index)
{
TouchPoint o = activeTouchPoint.get(index);
o.Kill();
inactiveTouchPoint.add(b);
activeTouchPoint.remove(b);
}
Now I think I have a problem here right?
If I am looping through my list of active TouchPoints and updating/checking them. I have the potential to throw an exception, because it would be possible to be in the middle of updating/checking the active TouchPoints, but then at the same time the Listener could be triggered by removing a finger from the screen. Causing a TouchPoint to be removed from the active list, and then my loop explodes on being out of bounds because I'm still tyring to update/check my active TouchPoints.
How can I stop this? What is the best solution?
Could I just loop through my array list backwards? Does this call for the synchronized keyword?
Also can someone explain to me what the synchronize keyword actually does?
I understand its like a semaphore, but what happens if something already has the lock? Does the syncronized code section get skipped over or do I block until the lock becomes available?