Java 线程——Swing 事件调度线程中的永久循环
我有一个 SwingWorker
及其实现:
public Void doInBackground() throws InterruptedException
{
while (true)
{
while(_update)
{
}
}
}
如果 _update 设置为 false,让 EDT 永久循环是否安全?或者我应该让它每秒睡觉?
-- 编辑
看起来我没有使用 SwingWorker,因为它应该被使用。谢谢大家。我将坚持根据需要创建 SwingWorkers。
I have a SwingWorker
with the implementation:
public Void doInBackground() throws InterruptedException
{
while (true)
{
while(_update)
{
}
}
}
If _update is set to false, is it safe to let the EDT perpetually go on a loop? Or should I let it sleep every second?
-- EDIT
It looks like I'm not using SwingWorker as it's supposed to be used. Thanks all. I'll stick to creating SwingWorkers as I need them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让事件分派线程循环运行是不安全的。
需要事件分派线程来提供用户与应用程序的交互。如果您希望某个事件每隔几秒发生一次,则应在每次完成时将其或其另一个副本放回到事件队列中。
It is not safe to let the Event Dispatch Thread go in a loop.
The event dispatch thread is needed to provide user interaction with the application. If you want an event to go off every few seconds, you should put it, or another copy of it, back on the event queue each time it has completed.
为什么不启动另一个线程并让它根据需要运行/睡眠,并让它使用 SwingUtilities.InvokeLater 或其他任何必要的东西来执行 UI 工作?
如果没有任何其他上下文,看起来您可能会滥用 SwingWorker?
Why wouldn't you just start another thread and let it run/sleep as needed, and have it use SwingUtilities.InvokeLater or whatever to do UI work as necessary?
Without any other context it looks like you might be misusing SwingWorker?