我尝试从可调用更新 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();
}
}
关于java - GUI 线程作业不是从可调用对象调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14646450/