Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About the EDT (Java)

I have read a number of articles on the internet about when something should run in the EDT, and when it shouldn't. But I'm still not sure I understand, so I'd like to ask a few question about this:

  1. What pieces of code are going to run by default inside the EDT?

  2. What pieces of code are going to be run be default outside the EDT?

  3. When should I use InvokeLater() so something that by default would run outside the EDT, will run inside it?

  4. When should I prevent a piece of code from running (by default) inside the EDT, by creating a new thread and putting that code inside it?

Thanks

like image 579
user3150201 Avatar asked Jun 26 '26 13:06

user3150201


1 Answers

  1. All the code executed by an event listener.
  2. The code in your main method, the code executed inside a thread that you explicitely started, or that has been started by the usage of a Timer or SwingWorker.
  3. When creating a Swing GUI in your main method. Or when you want to interact with a Swing component (or its model) from inside a background thread.
  4. When this piece of code is blocking (like long IO) or is taking more than a few milliseconds to execute. All the code executed from inside the EDT prevent this thread from doing its main job: repainting the GUI and reacting to events.
like image 149
JB Nizet Avatar answered Jun 29 '26 04:06

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!