Swiftslide
Swiftslide

Reputation: 41

Using a timer to start a SwingWorker?

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:

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

Upvotes: 4

Views: 2097

Answers (4)

Jim
Jim

Reputation: 3694

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

Upvotes: 1

Qwerky
Qwerky

Reputation: 18445

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.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I don't see why you couldn't use a Swing Timer to start a Swing Worker. What have you tried?

Upvotes: 2

pintxo
pintxo

Reputation: 2155

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);

Upvotes: 2

Related Questions