summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java')
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java59
1 files changed, 47 insertions, 12 deletions
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 488331a..233c415 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java
@@ -8,10 +8,12 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove;
8import com.fourisland.fourpuzzle.Game; 8import com.fourisland.fourpuzzle.Game;
9import com.fourisland.fourpuzzle.gamestate.mapview.event.Event; 9import com.fourisland.fourpuzzle.gamestate.mapview.event.Event;
10import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; 10import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
11import java.util.ArrayList;
11import java.util.List; 12import java.util.List;
12import java.util.Vector; 13import java.util.Vector;
13import java.util.concurrent.Executor; 14import java.util.concurrent.ExecutorService;
14import java.util.concurrent.Executors; 15import java.util.concurrent.Executors;
16import java.util.concurrent.Future;
15import java.util.concurrent.Semaphore; 17import java.util.concurrent.Semaphore;
16 18
17/** 19/**
@@ -20,10 +22,11 @@ import java.util.concurrent.Semaphore;
20 */ 22 */
21public class MoveEventThread implements Runnable { 23public class MoveEventThread implements Runnable {
22 24
23 private static Executor moveEventExecutor = Executors.newCachedThreadPool(); 25 private static ExecutorService moveEventExecutor = Executors.newCachedThreadPool();
24 26
25 private static volatile List<Event> events = new Vector<Event>(); 27 private static volatile List<Event> events = new Vector<Event>();
26 private static volatile Semaphore moveEventWait = new Semaphore(100); 28 private static volatile Semaphore moveEventWait = new Semaphore(100);
29 private static volatile List<Future> eventThreads = new ArrayList<Future>();
27 30
28 private Event ev; 31 private Event ev;
29 private MoveEvent[] actions; 32 private MoveEvent[] actions;
@@ -36,7 +39,15 @@ public class MoveEventThread implements Runnable {
36 39
37 public void start() 40 public void start()
38 { 41 {
39 moveEventExecutor.execute(this); 42 for (Future f : eventThreads)
43 {
44 if (f.isDone())
45 {
46 eventThreads.remove(f);
47 }
48 }
49
50 eventThreads.add(moveEventExecutor.submit(this));
40 } 51 }
41 52
42 public void run() 53 public void run()
@@ -44,7 +55,7 @@ public class MoveEventThread implements Runnable {
44 try { 55 try {
45 moveEventWait.acquire(); 56 moveEventWait.acquire();
46 } catch (InterruptedException ex) { 57 } catch (InterruptedException ex) {
47 Thread.currentThread().interrupt(); 58 return;
48 } 59 }
49 60
50 while (ev.isMoving()) 61 while (ev.isMoving())
@@ -52,25 +63,49 @@ public class MoveEventThread implements Runnable {
52 try { 63 try {
53 Thread.sleep(2); 64 Thread.sleep(2);
54 } catch (InterruptedException ex) { 65 } catch (InterruptedException ex) {
55 Thread.currentThread().interrupt(); 66 return;
56 } 67 }
57 } 68 }
58 69
59 events.add(ev); 70 events.add(ev);
60 71
61 for (MoveEvent action : actions) 72 try
62 { 73 {
63 action.doAction(ev); 74 for (MoveEvent action : actions)
64 } 75 {
76 action.doAction(ev);
65 77
66 events.remove(ev); 78 if (Thread.currentThread().isInterrupted())
67 moveEventWait.release(); 79 {
80 throw new InterruptedException();
81 }
82 }
83 } catch (InterruptedException ex)
84 {
85 /* Swallow the interrupt because execution will drop to the finally
86 * and then the method will end anyway */
87 } finally {
88 events.remove(ev);
89 moveEventWait.release();
90 }
68 } 91 }
69 92
70 public static void moveAll() throws InterruptedException 93 public static void moveAll() throws InterruptedException
71 { 94 {
72 moveEventWait.acquire(100); 95 try
73 moveEventWait.release(100); 96 {
97 moveEventWait.acquire(100);
98 } catch (InterruptedException ex)
99 {
100 for (Future f : eventThreads)
101 {
102 f.cancel(true);
103 }
104
105 throw ex;
106 } finally {
107 moveEventWait.release(100);
108 }
74 } 109 }
75 110
76 public static boolean isHeroActive() 111 public static boolean isHeroActive()