有没有更优雅的方式来做我下面正在做的事情呢?也就是说,有没有比轮询和休眠、轮询和休眠等等更优雅的方法来了解何时通过invokeLater()调用了Runnable.run()方法
private int myMethod() {
final WaitForEventQueue waitForQueue = new WaitForEventQueue();
EventQueue.invokeLater(waitForQueue);
while (!waitForQueue.done) {
try {
Thread.sleep(10);
} catch (InterruptedException ignore) {
}
}
return 0;
}
private class WaitForEventQueue implements Runnable {
private boolean done;
public void run() {
// Let all Swing text stuff finish.
done = true;
}
}发布于 2010-05-11 02:38:42
如果你想等待,为什么不调用invokeAndWait而自己实现它呢?
发布于 2010-05-11 02:45:00
一种更好的方法是使用FutureTask (它实现了Runnable和Future),并在完成时重写它的done()事件来做一些事情。
此外,将其作为单独的线程启动(或者如果它不执行图形用户界面操作,则使用Executor),而不是使用AWT EventQueue。
发布于 2010-05-11 02:34:51
与其等待线程完成,为什么不让UI显示一个微调器或其他东西,并在完成时让线程调用一个事件。
https://stackoverflow.com/questions/2805378
复制相似问题