mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Prevents multiple triggers fired on upkeep (won't execute handleBeginPhase more than once)
FThreads - more beautiful names for threads
This commit is contained in:
@@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.swing.SwingUtilities;
|
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() );
|
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 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 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; }
|
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
|
// This pool is designed to parallel CPU or IO intensive tasks like parse cards or download images, assuming a load factor of 0.5
|
||||||
|
|||||||
@@ -231,17 +231,30 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable {
|
|||||||
this.bRepeat = true;
|
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>
|
* <p>
|
||||||
* handleBeginPhase.
|
* handleBeginPhase.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public final void handleBeginPhase() {
|
private final void doBeginPhase() {
|
||||||
if (null == playerTurn) {
|
if (null == playerTurn) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setPhaseEffects(false);
|
|
||||||
// Handle effects that happen at the beginning of phases
|
// Handle effects that happen at the beginning of phases
|
||||||
game.getAction().checkStateEffects();
|
game.getAction().checkStateEffects();
|
||||||
|
|
||||||
@@ -823,7 +836,8 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable {
|
|||||||
*/
|
*/
|
||||||
public final void setPhaseState(final PhaseType phase0) {
|
public final void setPhaseState(final PhaseType phase0) {
|
||||||
this.phase = phase0;
|
this.phase = phase0;
|
||||||
this.handleBeginPhase();
|
this.setPhaseEffects(false);
|
||||||
|
this.doBeginPhase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -59,16 +59,9 @@ public class InputProxy implements Observer {
|
|||||||
System.out.printf("%s > InputProxy.update() =>%n", FThreads.debugGetCurrThreadId());
|
System.out.printf("%s > InputProxy.update() =>%n", FThreads.debugGetCurrThreadId());
|
||||||
|
|
||||||
if ( game.getInputQueue().isEmpty() && ph.hasPhaseEffects()) {
|
if ( game.getInputQueue().isEmpty() && ph.hasPhaseEffects()) {
|
||||||
Runnable rPhase = new Runnable() {
|
if(INPUT_DEBUG)
|
||||||
@Override
|
System.out.printf("\t%s > handle begin phase during %s%n", FThreads.debugGetCurrThreadId(), ph.debugPrintState());
|
||||||
public void run() {
|
ph.handleBeginPhase();
|
||||||
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);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user