2

I have a SwingWorker with the implementation:

public Void doInBackground() throws InterruptedException
{
     while (true)
     {
            while(_update)
            {

            }
     }
}

If _update is set to false, is it safe to let the EDT perpetually go on a loop? Or should I let it sleep every second?

-- EDIT

It looks like I'm not using SwingWorker as it's supposed to be used. Thanks all. I'll stick to creating SwingWorkers as I need them.

  • I don't understand why you are mucking with the EDT at all? and where in that code are you involved with the EDT? – hvgotcodes Dec 08 '10 at 22:18
  • 1
    Why not just let the thread end when _update is set to false, and spawn a new one when you set it to true again? – Karl Knechtel Dec 08 '10 at 22:19

2 Answers2

3

It is not safe to let the Event Dispatch Thread go in a loop.

The event dispatch thread is needed to provide user interaction with the application. If you want an event to go off every few seconds, you should put it, or another copy of it, back on the event queue each time it has completed.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
0

Why wouldn't you just start another thread and let it run/sleep as needed, and have it use SwingUtilities.InvokeLater or whatever to do UI work as necessary?

Without any other context it looks like you might be misusing SwingWorker?

John Gardner
  • 24,225
  • 5
  • 58
  • 76