Search code examples
javaswingsleepswingworker

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


Solution

  • 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.