From c791c138d4d73b495c8a355ae01bca754e1ce66b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 3 Feb 2009 17:31:19 -0500 Subject: Fixed MoveEventThread latch breakage Previously, MoveEventThread used a CountDownLatch to power it's moveAll() function. However, because CountDownLatch can only count down and not back up, every time a MoveEventThread was spawned, it's static moveEventWait (which was a CountDownLatch) would be re-instantated. If a thread was already waiting on the CountDownLatch, it would be disrupted. This meant that if the first MoveEventThread spawned completed before the rest, the entire program would grind to a halt. This has been fixed by replacing the CountDownLatch with a Semaphore (not exactly what Semaphore is supposed to be used for, but it works). Every time a MoveEventThread is spawned, it acquires a permit from the Semaphone and releases it when it completes. moveAll() then attempts to acquire 100 permits (the Semaphore is initalized to only allow 100 permits), which blocks until all MoveEventThreads have completed. The downside of this is that only 100 MoveEventThreads can execute at once, but it is exceptionally unlikely that such an event will occur. Also, if this does occur, the other MoveEventThreads will simply wait for another to complete and then acquire a permit. --- .../mapview/event/specialmove/MoveEventThread.java | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/com/fourisland') diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java index df4f4ee..2395dd0 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java @@ -10,7 +10,7 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.Event; import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; import java.util.List; import java.util.Vector; -import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; import java.util.logging.Level; import java.util.logging.Logger; @@ -19,14 +19,12 @@ import java.util.logging.Logger; * @author hatkirby */ public class MoveEventThread implements Runnable { + + private static volatile List events = new Vector(); + private static volatile Semaphore moveEventWait = new Semaphore(100); - public static volatile int countMoveEventThreads = 0; - - static volatile List events = new Vector(); - static volatile CountDownLatch moveEventWait = new CountDownLatch(0); - - Event ev; - MoveEvent[] actions; + private Event ev; + private MoveEvent[] actions; public MoveEventThread(Event ev, MoveEvent[] actions) { @@ -36,6 +34,12 @@ public class MoveEventThread implements Runnable { public void run() { + try { + moveEventWait.acquire(); + } catch (InterruptedException ex) { + Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex); + } + while (ev.isMoving()) { try { @@ -47,18 +51,13 @@ public class MoveEventThread implements Runnable { events.add(ev); - MoveEventThread.countMoveEventThreads++; - moveEventWait = new CountDownLatch(countMoveEventThreads); - for (MoveEvent action : actions) { action.doAction(ev); } - + events.remove(ev); - - MoveEventThread.countMoveEventThreads--; - moveEventWait.countDown(); + moveEventWait.release(); } /* TODO Rename the two following methods (isHeroMoving and isOtherMoving) @@ -78,7 +77,8 @@ public class MoveEventThread implements Runnable { public static void moveAll() { try { - moveEventWait.await(); + moveEventWait.acquire(100); + moveEventWait.release(100); } catch (InterruptedException ex) { Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex); } -- cgit 1.4.1