Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleeping in a SwingWorker?

I need to feed test data to a Swing interval over a time period. The data set is long to parse, so I'm using a SwingWorker to parse it in the background. When it comes to feeding the data to the GUI (say, one item per second), I could start a Timer from the SwingWorker, but this seems overkill. Any reasons not to sleep from within the SwingWorker itself?

Cheers

like image 639
Rom1 Avatar asked Jun 07 '11 12:06

Rom1


2 Answers

Since SwingWorker doesn't run on the EDT, sleeping within is perfectly fine.

SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing.

In my opinion, a Timer in addition to a SwingWorker would be overkill. Instead, just publish the parsed test data whenever possible, even at irregular intervals.

like image 197
mre Avatar answered Oct 20 '22 07:10

mre


While you make the SwingWorker sleep it will not be loading data. If that is desired then this approach is fine.

If you want to keep loading the data and just pace the feeding to the GUI then it seems like a better design would be to start a Timer in the GUI that would check and pull new data from the worker at an interval, instead of the worker having to make invokeLater calls back to the GUI.

like image 3
jzd Avatar answered Oct 20 '22 08:10

jzd