Prevents multiple triggers fired on upkeep (won't execute handleBeginPhase more than once)

FThreads - more beautiful names for threads
This commit is contained in:
Maxmtg
2013-05-25 19:41:24 +00:00
parent dc4f0e74ed
commit 4972fce2de
3 changed files with 37 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import javax.swing.SwingUtilities;
@@ -19,11 +20,24 @@ public class FThreads {
System.out.printf("(FThreads static ctor): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() );
}
private static class WorkerThreadFactory implements ThreadFactory {
private int countr = 0;
private String prefix = "";
public WorkerThreadFactory(String prefix) {
this.prefix = prefix;
}
public Thread newThread(Runnable r) {
return new Thread(r, prefix + "-" + countr++);
}
}
private FThreads() { } // no instances supposed
private final static ExecutorService cachedPool = Executors.newCachedThreadPool();
private final static ExecutorService cachedPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
private static ExecutorService getCachedPool() { return cachedPool; }
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2);
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
// This pool is designed to parallel CPU or IO intensive tasks like parse cards or download images, assuming a load factor of 0.5

View File

@@ -231,17 +231,30 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable {
this.bRepeat = true;
}
public final void handleBeginPhase() {
this.setPhaseEffects(false);
if ( FThreads.isEDT() ) {
System.out.printf("Handle begin %s phase of %s turn from EDT%n", phase, playerTurn);
FThreads.invokeInNewThread(beginPhase);
} else {
beginPhase.run();
}
}
private final Runnable beginPhase = new Runnable() { @Override public void run() {
doBeginPhase();
updateObservers();
} };
/**
* <p>
* handleBeginPhase.
* </p>
*/
public final void handleBeginPhase() {
private final void doBeginPhase() {
if (null == playerTurn) {
return;
}
this.setPhaseEffects(false);
// Handle effects that happen at the beginning of phases
game.getAction().checkStateEffects();
@@ -823,7 +836,8 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable {
*/
public final void setPhaseState(final PhaseType phase0) {
this.phase = phase0;
this.handleBeginPhase();
this.setPhaseEffects(false);
this.doBeginPhase();
}
/**

View File

@@ -59,16 +59,9 @@ public class InputProxy implements Observer {
System.out.printf("%s > InputProxy.update() =>%n", FThreads.debugGetCurrThreadId());
if ( game.getInputQueue().isEmpty() && ph.hasPhaseEffects()) {
Runnable rPhase = new Runnable() {
@Override
public void run() {
if(INPUT_DEBUG)
System.out.printf("\t%s > handle begin phase during %s%n", FThreads.debugGetCurrThreadId(), ph.debugPrintState());
ph.handleBeginPhase();
update(null, null);
}
};
FThreads.invokeInNewThread(rPhase);
if(INPUT_DEBUG)
System.out.printf("\t%s > handle begin phase during %s%n", FThreads.debugGetCurrThreadId(), ph.debugPrintState());
ph.handleBeginPhase();
return;
}