使用计时器启动 SwingWorker?

发布于 2024-11-07 15:17:53 字数 596 浏览 5 评论 0原文

我需要执行的任务包括从外部服务器请求一些数据,对数据执行一些(相当长的)处理,然后使用处理结果更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短叹 2024-11-14 15:17:53

您可以使用 ScheduledExecutorService :

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 

// get a scheduled executor service with one thread
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
// schedule the worker at an intervall of 5 seconds
scheduler.scheduleAtFixedRate(myWorker, 0, 5, TimeUnit.SECONDS);

You could use a ScheduledExecutorService:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 

// get a scheduled executor service with one thread
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
// schedule the worker at an intervall of 5 seconds
scheduler.scheduleAtFixedRate(myWorker, 0, 5, TimeUnit.SECONDS);
离鸿 2024-11-14 15:17:53

我不明白为什么你不能使用 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

山田美奈子 2024-11-14 15:17:53

您正在检索的数据集有多大?如果它相当小,我会完全分离获取/处理和显示的任务。

  1. 使用某种内存缓存来保存最近处理的数据集。
  2. 使用 javax.swing.Timer 使用缓存数据更新 GUI。
  3. 使用java.util.Timer从数据库中获取数据、处理数据并更新缓存。
  4. 请注意缓存上两个时间之间的同步问题。您不希望您的摆动计时器在其他计时器更新数据的同时抓取数据。

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.

  1. Use some sort of in memory cache to hold the most recently processed set of data.
  2. Use a javax.swing.Timer to update the GUI with the cached data.
  3. Use a java.util.Timer to fetch the data from the database, process it and update the cache.
  4. Be careful of synchronisation issues between the two times on your cache. You don't want your swing timer grabbing data at the same time as the other timer is updating it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文