我正在尝试从可调用对象更新 UI,但失败了。为什么System.out.println("UI thread.");
从不打电话?我正在使用SWT。
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("Executing Callable.");
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
System.out.println("UI thread.");
}
});
System.out.println("End.");
return true;
}
};
ExecutorService executor = Executors.newFixedThreadPool(10);
executor.submit(callable);
问题发生了,因为我在JunitTest中执行了上层代码。在异步 Runnable 可以执行之前返回的测试。在测试结束时等待 - 帮助!
@Test
public void jobs() {
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("Executing Callable.");
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
System.out.println("UI thread.");
}
});
System.out.println("End.");
return true;
}
};
ExecutorService executor = Executors.newFixedThreadPool(10);
executor.submit(callable);
long stopTimestampt = System.currentTimeMillis() + 1000;
while (System.currentTimeMillis() < stopTimestampt) {
Display.getDefault().readAndDispatch();
}
}