使用计时器启动 SwingWorker?
我需要执行的任务包括从外部服务器请求一些数据,对数据执行一些(相当长的)处理,然后使用处理结果更新 GUI。由于服务器可能没有响应,因此该任务非常适合 SwingWorker:doInBackground() 方法获取结果,然后 did 方法更新 GUI。
我需要这种情况每隔几秒发生一次。我知道我可以使用 while 循环和 Thread.sleep,并在每次睡眠后创建一个新的 SwingWorker。但我读过的所有内容都反对使用循环和睡眠。我想使用计时器,但是:
使用摆动计时器似乎会适得其反;因为它们在 EDT 上运行,所以我基本上没有理由使用 SwingWorker 的 doInBackground 方法。如果服务器没有响应,GUI 将没有响应。
使用 java.util.Timer 似乎有点浪费:它似乎为 TimerTask() 创建了一个后台线程,并且由于我只是创建一个 SwingWorker 来完成实际工作,所以我本质上是在创建一个后台线程这会创建另一个后台线程。
谁能告诉我最好的解决方案是什么?我想坚持使用 SwingWorker,因为它似乎非常适合该任务,但如果可以的话,我想避免使用 while 循环。
谢谢
The task I need to perform involves requesting some data from an external server, performing some (fairly lengthy) processing on the data, and then updating the GUI with the results of the processing. Because the server might be unresponsive, the task is well suited to a SwingWorker: the doInBackground()
method gets the results, and then the done method updates the GUI.
I need this to happen once every few seconds. I know I can just use a while loop and Thread.sleep, and create a new SwingWorker after each sleep. But everything I've read frowns upon using loops and sleep. I'd like to use a timer but:
Using a swing timer seems counterproductive; since they run on the EDT, I would essentially have no reason to use SwingWorker's doInBackground method. If the server were not responsive, the GUI would be unresponsive.
Using a java.util.Timer seems a bit wasteful: it seems to create a background thread for the TimerTask(), and since I am just creating a SwingWorker to do the actual work, I'm essentially creating a background thread that creates another background thread.
Can anybody tell me what's the best solution? I'd like to stick with SwingWorker since it seems ideally suited to the task, but I would like to avoid using a while loop if I can help it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 ScheduledExecutorService :
You could use a ScheduledExecutorService:
我不明白为什么你不能使用 Swing Timer 来启动 Swing Worker。你尝试了什么?
I don't see why you couldn't use a Swing Timer to start a Swing Worker. What have you tried?
我认为您使用 SwingWorker 的方向是正确的。现在您需要查看它的发布和处理方法。随着处理的进行,您从后台线程发布()一个对象,然后在 Swing(EDT)线程上调用 process()方法,以便您可以更新 gui。
这样就没有一堆计时器和其他线程需要协调。
javadoc 有一个简单的质数示例:
http://download.oracle.com/javase/6 /docs/api/javax/swing/SwingWorker.html
I think you're on the right track with SwingWorker. Now you need to look at its publish and process methods. As your processing progresses, you publish() an object from the background thread then the process() method is called on the Swing(EDT) thread so you can update the gui.
This way there aren't a bunch of timers and other threads to coordinate.
The javadocs have a straightforward example with prime numbers:
http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html
您正在检索的数据集有多大?如果它相当小,我会完全分离获取/处理和显示的任务。
javax.swing.Timer
使用缓存数据更新 GUI。java.util.Timer
从数据库中获取数据、处理数据并更新缓存。How large is the set of data you are retrieving? If it is fairly small I would completely detach the task of fetching/processing and displaying.
javax.swing.Timer
to update the GUI with the cached data.java.util.Timer
to fetch the data from the database, process it and update the cache.