3

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

Ben
  • 51,770
  • 36
  • 127
  • 149
user3150201
  • 1,901
  • 5
  • 26
  • 29

2 Answers2

4
  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.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for the answer. About answer 1: Do you mean the the ONLY type of code that will be run be default in the EDT, is code in actionPerformed() methods? Nothing else? – user3150201 Jan 03 '14 at 10:57
  • Not only actionPerformed(). All the event-handling methods of the other kinds of listeners as well (ItemListener, MouseListener, SelectionListener, etc.). And of course, it's transitive: if actionPerformed() calls foo(), which calls bar(), which calls baz(), all these methods are executed in the EDT. In a typical GUI application, almost everything is executed as a response to an event. – JB Nizet Jan 03 '14 at 11:01
  • Okay. Is calling repaint() on a JPanel-extending class run in the EDT? – user3150201 Jan 03 '14 at 11:02
  • Impossible to say. Print a stack trace, and you'll know from where this repaint() call comes from. Or use a debugger to find out. A single method can be called from many threads. repaint() can be called safely from any thread. – JB Nizet Jan 03 '14 at 11:04
  • basically 1) is "anything to do with the Swing API", but that is perhaps a tad too generalized. paint() actions indeed also happen there for example, but not at the exact moment that you would call repaint(). SwingWorker and SwingUtilities.invokeLater() are more ways to get things to run on the EDT. – Gimby Jan 03 '14 at 11:04
  • Let's say I got a game with a game-loop that runs every 10 milliseconds. At the end of this game-loop there is repaint(), which means every 10 ms repaint() is called. Should I put repaint() in a different thread so it will not be run in the EDT? – user3150201 Jan 03 '14 at 11:07
  • Your game loop is already a background thread, different from the EDT. You can safely call repaint() from this background thread. This will simply ask the EDT to repaint the GUI, some time later. As a result, some time later, the paintComponent() method of the panel will be called by Swing, inside the EDT. – JB Nizet Jan 03 '14 at 11:09
  • in Java6 repaint() works, but in Java7 are some changes in APIs and almost all thread safe methods in Java6 aren't no longer thread safe, here is an bunch of post about, to avoiding any speculations about to use Swing Timer with Swing Action/ActionListener – mKorbel Jan 03 '14 at 11:16
  • you can't loop on perion at 10 milliseconds on standard PC, this is under Latency for standard Native OS (this value is better in Win8/8.1), use 25/33 as standard – mKorbel Jan 03 '14 at 11:18
  • you can to test isEventDispatchThread, have to accept that logics for EDT is very simple, EDT exist, returns true untill one event is pressent in queue, doeasn't exist, returns false in the case that all events are flushed, executed or terminated max value is 10-15 seconds – mKorbel Jan 03 '14 at 11:20
0

First of all thank you so much for editing and formatting your question very well. It helps a lot when answering your question.

Also i have to admit i am not 100% sure about my answers, so guys: feel free to correct me if i am wrong.

  1. Everything which changes your graphical user interface.

  2. Not quite sure about that.

  3. If you need to update your gui with the time intensive calculations. For example if you want to show the numbers from 0to 100000000 in a JLabel.

  4. Everything which would block your gui from user interaction because it takes a lot of time, for example some calculations with a lot of datasets.. But you need to make sure to access values only from one thread or to synchronize the threads with volatile and
    synchronize...

dehlen
  • 7,325
  • 4
  • 43
  • 71
  • Your answers to 1 and 2 actually answer the question "what **should** run inside (or outside) the EDT". Not the OP's question which is "What runs, by default, inside or outside the EDT". – JB Nizet Jan 03 '14 at 10:55
  • yep i got confused by the numbers and edited my answer... i think now it should be correct, but i think your answers are much more precisely so i upvoted you ;-) – dehlen Jan 03 '14 at 10:56