3

I know the Swing single-thread rule (from Java Concurrency in Practice):

Swing components and models should be created, modified, and queried only from the event-dispatching thread.

Is the converse also true? I maintain some transaction logging code that writes event information to a file and it sometimes does this on the EDT. Is this a bad practice?

Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
  • 6
    Long running tasks should not be executed on the EDT. It will cause the UI to become unresponsive. It's not a necessity, but your customers will appreciate it. – mre May 29 '14 at 15:03
  • @Paul Reiners wrote - `to a file and it sometimes does this on the EDT.`, this isn't right, you have to decide, if its always ended on the EDT or never, then after you can to choose which of methods you have to use, nothing in the middle, – mKorbel May 29 '14 at 17:08
  • 1
    By long running, we mean code that takes more than 300 milliseconds to run. – Gilbert Le Blanc May 29 '14 at 17:31

1 Answers1

5

That depends on what you are doing. Basically while you are using the EDT thread then it cannot do anything else. That means button clicks, processing, user interface updates, etc will all be stalled.

So for long-running tasks you should use a different thread (for example SwingWorker) but for anything that is unlikely to stall or take a long time doing it on the EDT is fine.

Tim B
  • 40,716
  • 16
  • 83
  • 128