16

1.Consider my code is on some line of a JPanel that I have, am I automatically on EDT?

2.Same question for all other classes which are not belong to GUI, JPanels or other view classes, simple logical class.

3.If I have JPanel which I'm playing music while being in it, should the music run on event dispatch thread or on other thread which is not EDT (for not blocking the GUI, although I didn't feel any problem with running it from EDT)?

Note: I want a general rule how to know it without using the SwingUtilities.isEventDispatchThread()
Thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • 3
    When all else fails, the [SwingUtilities](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#isEventDispatchThread()) class has a static method that you can use: `SwingUtilities.isEventDispatchThread()` – Hovercraft Full Of Eels Oct 22 '11 at 23:19
  • Yes I know, I forgot to write that I want a general rule how to know it without using the SwingUtilities.isEventDispatchThread() – JavaSa Oct 22 '11 at 23:21
  • 2
    @mKorbel has posed this [question](http://stackoverflow.com/questions/7787998/how-to-generate-exceptions-from-repaintmanager) referencing an article that summarizes several approaches to finding EDT violations. – trashgod Oct 23 '11 at 01:17
  • 2
    why do you _not_ want to use api that is meant to solve your problem? (which I assume is guarding against violations of the EDT rule) – kleopatra Oct 23 '11 at 11:34
  • @kleopatra: Which one do you refer to? – JavaSa Oct 23 '11 at 12:09
  • 1
    'which one' implies options - I see only one method mentioned in your question ;-) – kleopatra Oct 24 '11 at 07:45

2 Answers2

25
  1. No.
  2. No.
  3. Background thread.

If code running outside the EDT calls a method defined in a GUI class, that code will not be run on the EDT but in the calling thread.

If code running in the EDT calls code defined in a non-GUI class, that code will run on the EDT.

The rule is that if you're not creating a different thread, the method you're calling will run on the thread the calling code is running from – threads do not correspond to what classes methods are defined in.

Methods that will run on the EDT are event listeners, when they're called by Swing – not by you. (They still might be if you're calling them from the EDT though.)

Also, any code inside the Runnable.run() method passed to SwingUtilities.invokeLater() and invokeAndWait() is also run on the EDT.

Any normal methods you call from the EDT will run on the EDT.

Any code called from a Thread you create (whether using threads directly, or ExecutorService, or SwingWorker.doInBackground()) is not on the EDT. Your program's main() method is also not on the EDT.

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • 1
    For all other next readers of this post: It's also recommended to read luke's answer in the following post: http://stackoverflow.com/questions/7217013/java-event-dispatching-thread-explanation – JavaSa Oct 23 '11 at 11:01
  • Something is still strange When I'm some JPanel (EDT for sure-I checked with the method check) and then I call some animation thread(the thread extend Thread) to start, inside the thread I'm not on EDT by check. So I guess I should be because animation should be on EDT, so I wrapped the animate method with runnable and invokeAndWait(), but still got that in the animation thread I'm not on EDT, while calling to that code as I said earlier is on EDT, so, my invokeAnd wait seems not to place that animation on EDT? why is that? – JavaSa Oct 23 '11 at 16:32
  • @JavaSa: Post this as a separate question, including the relevant code. Also add a comment with the link to the question here. – millimoose Oct 23 '11 at 17:05
  • Thanks, here it is http://stackoverflow.com/questions/7867960/animation-thread-and-edt – JavaSa Oct 23 '11 at 18:01
10

As per my comment: When all else fails, the SwingUtilities class has a static method that you can use: SwingUtilities.isEventDispatchThread()

With regards to number 3) definitely use a background thread.

And as far as I know, there is no "general rule". Yes code in your GUI should be on the EDT, but if you have a bug somewhere, it may not be, though usually it is. Same for Swing listeners.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373