mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 20:58:03 +00:00
Fix threading issues with wait callbacks
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -1186,6 +1186,7 @@ forge-gui-mobile/src/forge/toolbox/ListChooser.java -text
|
||||
forge-gui-mobile/src/forge/util/LayoutHelper.java -text
|
||||
forge-gui-mobile/src/forge/util/PhysicsObject.java -text
|
||||
forge-gui-mobile/src/forge/util/Utils.java -text
|
||||
forge-gui-mobile/src/forge/util/WaitRunnable.java -text
|
||||
forge-gui/.classpath -text
|
||||
forge-gui/.project -text
|
||||
forge-gui/.settings/org.eclipse.core.resources.prefs -text
|
||||
|
||||
@@ -5,8 +5,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
@@ -46,6 +44,7 @@ import forge.toolbox.FOptionPane;
|
||||
import forge.toolbox.GuiChoose;
|
||||
import forge.util.ThreadUtil;
|
||||
import forge.util.WaitCallback;
|
||||
import forge.util.WaitRunnable;
|
||||
|
||||
public class GuiMobile implements IGuiBase {
|
||||
@Override
|
||||
@@ -59,20 +58,12 @@ public class GuiMobile implements IGuiBase {
|
||||
proc.run();
|
||||
}
|
||||
else {
|
||||
final CountDownLatch cdl = new CountDownLatch(1);
|
||||
Gdx.app.postRunnable(new Runnable() {
|
||||
new WaitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
proc.run();
|
||||
cdl.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
cdl.await();
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}.invokeAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package forge.toolbox;
|
||||
|
||||
import forge.FThreads;
|
||||
import forge.game.card.Card;
|
||||
import forge.util.Callback;
|
||||
|
||||
@@ -48,11 +47,6 @@ public class GuiDialog {
|
||||
}
|
||||
|
||||
public static void message(final String message, final String title) {
|
||||
FThreads.invokeInEdtAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FOptionPane.showMessageDialog(message, title, null);
|
||||
}
|
||||
});
|
||||
FOptionPane.showMessageDialog(message, title, null);
|
||||
}
|
||||
}
|
||||
|
||||
33
forge-gui-mobile/src/forge/util/WaitRunnable.java
Normal file
33
forge-gui-mobile/src/forge/util/WaitRunnable.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package forge.util;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
|
||||
import forge.FThreads;
|
||||
|
||||
public abstract class WaitRunnable implements Runnable {
|
||||
public class Lock {
|
||||
}
|
||||
|
||||
private final Lock lock = new Lock();
|
||||
|
||||
public void invokeAndWait() {
|
||||
FThreads.assertExecutedByEdt(false);
|
||||
Gdx.app.postRunnable(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
WaitRunnable.this.run();
|
||||
synchronized(lock) {
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
synchronized(lock) {
|
||||
lock.wait();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,32 @@ package forge.util;
|
||||
import forge.FThreads;
|
||||
|
||||
public abstract class WaitCallback<T> extends Callback<T> implements Runnable {
|
||||
public class Lock {
|
||||
}
|
||||
|
||||
private final Lock lock = new Lock();
|
||||
|
||||
private T result;
|
||||
|
||||
@Override
|
||||
public final void run(T result0) {
|
||||
result = result0;
|
||||
synchronized(lock) {
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public final T invokeAndWait() {
|
||||
FThreads.assertExecutedByEdt(false); //not supported if on UI thread
|
||||
FThreads.invokeInEdtAndWait(this);
|
||||
FThreads.invokeInEdtLater(this);
|
||||
try {
|
||||
synchronized(lock) {
|
||||
lock.wait();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user