Java在新线程上运行javax.swing.Timer? [英] Java does javax.swing.Timer run on new thread?

查看:276
本文介绍了Java在新线程上运行javax.swing.Timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javax.swing.Timer来安排和运行事件。但它似乎冻结了GUI。只是想知道这些事件是在单独的线程上运行还是我必须自己运行。

I am using javax.swing.Timer to schedule and run events. But it seems to be freezing the GUI. Just wanted to know whether these events are run on a seperate thread or whether I have to do it myself.

谢谢

推荐答案

尽管所有计时器都使用单个共享线程(由执行的第一个Timer对象创建)执行等待,但计时器的操作事件处理程序在另一个线程上执行 - 事件调度线程。这意味着Timers的操作处理程序可以安全地对Swing组件执行操作。但是,这也意味着处理程序必须快速执行以保持GUI响应。 - javax.swing.Timer

"Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive."—javax.swing.Timer

强调增加。

附录: SwingWorker 是为这种情况而设计的。特别是,它提供了一种方便的卸载工作方式,同时定期在EDT上重建进度。

Addendum: SwingWorker was designed for just this scenario. In particular, it offers a convenient way to offload work while periodically reporing progress on the EDT.

这篇关于Java在新线程上运行javax.swing.Timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆