开发者

Sleeping in a SwingWorker?

开发者 https://www.devze.com 2023-03-11 04:25 出处:网络
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

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 th开发者_如何学JAVAe SwingWorker itself?

Cheers


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号