1

我正在尝试从可调用更新 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);
4

1 回答 1

0

问题发生了,因为我在 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();
 }
}
于 2013-02-01T13:29:07.987 回答