GUI线程作业不是从callable调用的

时间:2013-02-01 12:35:59

标签: java user-interface swt callable

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

1 个答案:

答案 0 :(得分: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();
 }
}