The task that I need to perform includes requesting some data from an external server, performing some (rather lengthy) data processing, and then updating the GUI with the processing results. Since the server may be unresponsive, the task is well suited for SwingWorker: the doInBackground() method gets the results, and then the ready-made method updates the GUI.
I need this to happen every few seconds. I know that I can use the while and Thread.sleep loops and create a new SwingWorker after each sleep. But everything I read frowns using loops and sleep. I would like to use a timer, but:
Using a swing timer seems counterproductive; since they work on EDT, I would essentially have no reason to use SwingWorker doInBackground. If the server did not respond, the GUI would not respond.
Using java.util.Timer seems a bit wasteful: it seems to create a background thread for TimerTask (), and since I just create a SwingWorker to do the actual work, I essentially create a background thread that creates another background thread.
Can someone tell me what is the best solution? I would like to stick with SwingWorker as it is perfect for the task, but I would like to avoid using a while loop if I can help it.
thanks
source share