Fix so Auto payment occurs in Game thread

This commit is contained in:
drdev
2013-12-12 11:49:50 +00:00
parent 1c391a7c7e
commit 849655cb2f
2 changed files with 38 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
package forge.util;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -7,11 +8,10 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class ThreadUtil {
static {
static {
System.out.printf("(ThreadUtil first call): 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 = "";
@@ -24,9 +24,7 @@ public class ThreadUtil {
return new Thread(r, prefix + "-" + countr++);
}
}
private final static ExecutorService gameThreadPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
private static ExecutorService getGameThreadPool() { return gameThreadPool; }
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
@@ -36,18 +34,40 @@ public class ThreadUtil {
public final static ExecutorService getComputingPool(float loadFactor) {
return Executors.newFixedThreadPool((int)(Runtime.getRuntime().availableProcessors() / (1-loadFactor)));
}
public static boolean isMultiCoreSystem() {
return Runtime.getRuntime().availableProcessors() > 1;
}
public static void invokeInGameThread(Runnable toRun) {
getGameThreadPool().execute(toRun);
}
public static void invokeInGameThreadAndWait(final Runnable toRun) {
if (isGameThread()) {
toRun.run(); //just run in the current thread
return;
}
final CountDownLatch latch = new CountDownLatch(1);
getGameThreadPool().execute(new Runnable() {
@Override
public void run() {
toRun.run();
latch.countDown();
}
});
try {
latch.await();
}
catch (final InterruptedException ex) {
throw new RuntimeException(ex);
}
}
public static void delay(int milliseconds, Runnable inputUpdater) {
getScheduledPool().schedule(inputUpdater, milliseconds, TimeUnit.MILLISECONDS);
}
public static boolean isGameThread() {
return Thread.currentThread().getName().startsWith("Game");
}