mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Fix threading issues with wait callbacks
This commit is contained in:
@@ -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