Switch game thread executor to be a single-thread

executor instead, of a thread pool. This is needed
because when things need to execute on the game
thread, it's not thread safe so only one such thing
should be executing at a time.

This should fix ConcurrentModificationException
from CardAreaPanel.selectCard() on mobile.

This is a reland of r34680 with a fix for being able
to run tasks on the game thread while waiting for input
from the user. Specifically, tested paying costs and
using the dev panel, but should work with anything that
schedules tasks via GameAction.invoke().
This commit is contained in:
Myrd
2017-07-13 05:03:34 +00:00
parent 7bc6761502
commit 1d3f91fb9c
3 changed files with 55 additions and 11 deletions

View File

@@ -17,6 +17,7 @@
*/
package forge.game;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
@@ -76,6 +77,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* Methods for common actions performed during a game.
@@ -1744,8 +1746,17 @@ public class GameAction {
// state effects are checked only when someone gets priority
}
// Invokes given runnable in Game thread pool - used to start game and perform actions from UI (when game-0 waits for input)
public void invoke(final Runnable proc) {
private Function<Runnable, Void> invokeFunction;
public synchronized void setInvokeFunction(Function<Runnable, Void> invokeFunction) {
this.invokeFunction = invokeFunction;
}
// Invokes given runnable on the Game thread - used to start game and perform actions from UI when waiting for input.
public synchronized void invoke(final Runnable proc) {
if (invokeFunction != null) {
invokeFunction.apply(proc);
return;
}
if (ThreadUtil.isGameThread()) {
proc.run();
}