gpt4 book ai didi

java - GUI 线程作业不是从可调用对象调用的

转载 作者:行者123 更新时间:2023-12-01 04:52:54 25 4
gpt4 key购买 nike

我尝试从可调用更新 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com