Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should non-Swing code be executed on non-EDT threads?

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?

like image 974
Paul Reiners Avatar asked Dec 22 '25 04:12

Paul Reiners


1 Answers

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.

like image 70
Tim B Avatar answered Dec 23 '25 19:12

Tim B