Fix threading issues with wait callbacks

This commit is contained in:
drdev
2014-04-15 04:32:31 +00:00
parent 8f95c98a3d
commit 5471cd32a4
5 changed files with 55 additions and 20 deletions

View File

@@ -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;
}
}