等待 AWT 事件调度程序线程 具有多线程的线程
有没有办法阻止 EDT 上的执行,同时仍然具有生动的 GUI?
我注意到 JDialog/JOptionPane 在调用 setVisible(true) 时有类似的东西。
我为什么需要这个?
我的类应该满足以下接口:
interface Quitable {
boolean quit();
}
我的类需要这样的实现:
class MyApp implements Quitable {
ExecutorService executor = Executors.newCachedThreadPool();
// ...
public boolean quit() {
FuturTask<Boolean> futureTask = new FutureTask<Boolean>(saveJob);
executor.execute(futureTask);
// TODO: keep gui vivid
boolean saveResult = futureTask.get();
return saveResult;
}
// ...
}
我需要返回保存过程的结果(这可能需要一些时间并且可能会失败)。或者您会建议另一种解决方案吗?感谢您的任何想法。 ;)
Is there a way to block the execution on the EDT, with still having a vivid gui?
I noticed that JDialog/JOptionPane has something similar, when calling setVisible(true).
Why would i need this?
My Class should satisfy to following interface:
interface Quitable {
boolean quit();
}
My Class needs a implementation like this:
class MyApp implements Quitable {
ExecutorService executor = Executors.newCachedThreadPool();
// ...
public boolean quit() {
FuturTask<Boolean> futureTask = new FutureTask<Boolean>(saveJob);
executor.execute(futureTask);
// TODO: keep gui vivid
boolean saveResult = futureTask.get();
return saveResult;
}
// ...
}
I need to return the result of the save process (which may take some time and may fail). Or would you suggest another solution? Thanks for any ideas. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dialog
导致事件循环在阻塞时执行。有一个过滤器允许某些事件通过,例如重绘请求、调用事件和对话窗口事件。此 API 并未公开。有一个库(Foxtrot,IIRC),它使用了
Dialog
上的 hack 来揭露这种行为。我强烈建议您以不需要这样做的方式构建您的程序。这是一个非常程序化的组织,而不是事件驱动的组织。因此,将您的任务排队到另一个线程(可能是线程池/执行程序服务或类似 GUI 的事件循环),然后使用 java.awt.EventQueue.invokeLater 更新 GUI。
Dialog
causes the an event loop to be executed whilst it is blocking. There is a filter that allows certain events through such as repaint requests, invocation events and events to the dialog window. The API for this is not exposed.There was a library (Foxtrot, IIRC), that used a hack on
Dialog
to expose this behaviour.I would strongly suggest structuring your program in such a way that this is not required. It's a very procedural rather than event-driven organisation. So queue your task on another thread (possible a thread pool/executor service or GUI-like event-loop), and then update the GUI with
java.awt.EventQueue.invokeLater
.JDialog 不会阻止 EDT 的执行,当它作为模态打开时,EDT 会忽略除 jDialog 事件之外的事件。
我确实建议您在调用 quit 方法之前将进度条显示为模式。
JDialog does not block the execution of EDT, EDT ignores events except jDialog's event when it is opened as modal.
I do suggest you to show progressbar as modal, before calling quit method.