0

My quest to understand swing and EDT continues once again...

Since EDT is EDT, the one and only, I would now like to know which methods, constructors and any other stuff is something that should be done in EDT. I know the general rule, almost all code that creates or interacts with Swing components must run on the event dispatch thread, but that is very general. It is also said that any non thread-safe swing code should be executed on EDT. But I still can't tell which methods are thread-safe, and which aren't.

My question is, is there a list of commands that will eventually be queued on the EDT? (I say that because you don't have to call repaint() from the EDT, but it will execute on it never the less).

If I knew where (and with that I can estimate when relative to the rest of the code) my methods will execute, I could make much more efficient and understandable code.

Most of my EDT work so far has been stabbing in the dark, thus making faulty code and then, when I couldn't figure it out, usually annoy the hell out of people here.

So is there a list, maybe something in Javadocs that I missed? Maybe some more specific rule (e.g., if methods has an "e" in it's name, it has to be executed on the EDT type of specific)?

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • 1
    The problem with the question, is what might be considered safe in one version of Java may not be in another. While annoying, the best bet is to always err on the side of caution and assume everything is not thread safe. The problem is not just that you ought get a race condition when updating a component between two threads, but that the repaint manager will try and repaint the component while its been mutated, causing strange paint artefacts – MadProgrammer Mar 31 '13 at 20:48
  • Well, let's tlak about Java 7 update 7 (that's the latest one, isn't it?) If I assume everything is not thread-safe, extending it to my logic, I would then execute it all in EDT (which is something everybody is trying to avoid, is it not?). – Karlovsky120 Mar 31 '13 at 20:55
  • Is there a way to find out if a method will eventually be executed on the EDT or not, if we know the version of Java? – Karlovsky120 Mar 31 '13 at 20:57
  • The only way to reasonably sure is to check the JavaDocs. But there have been instances where they have been documented incorrectly, so as I said, the safest cause of action is to generally accept that your UI code must execute within the context of the EDT - IMHO – MadProgrammer Mar 31 '13 at 21:05
  • 1
    _understand swing and EDT continues once again_ well, that's because you can't take NO for an answer ;-) Trying one last time: there's the EDT and you have to do *everything* swing view-related on that thread, *always* - with only a couple of documented exceptions (repaint, revalidate - nothing else that I'm aware of). Period, live with it, no option, no way around. Once you accept the fact (and I mean *really* accept it), life will become lighter again :) – kleopatra Apr 01 '13 at 11:06
  • I accepted the EDT as what it is. Now I just wanted to know what exactly is under it's jurisdiction. And you just eaid that everyting that is situated inside javax.swing has to be done in EDT? That's the only thing I asked in this question... I konw that even the exceptions will end up on EDT, though you don't have to call them from it. Thank you. Make this an answer and I'll mark it as the one I was looking for. – Karlovsky120 Apr 01 '13 at 14:25

1 Answers1

2
  • there isn't something complicated, strange, nor misterious, EDT is alive untill all events are done,

  • if all events in EDT are done then SwingUtilities.isEventDispatchThread() returns false, always

  • notice Mouse and Keys events can generating a new events to EDT(some JComponents reacting to those events internally, notifiers implemented in API firing a new event to EDT, then EDT is alive, for example JButton in container and without Focus firing events from ButtonModel, valid for Java6 and never in Win7 and newer), doesn't matter if is there added XxxListener, have to test without

  • invokeLater() alive EDT in all cases excluding freeze by using Thread.sleep(int)

  • Thread.sleep(int) can caused to lost all events during sleep(), or Swing GUI is refreshed only on Mouse_Hover_Over

EDIT

  • in Java7 is possible to creating SecondaryLoop, but I miss real reason for this interface, because all events still must be done on EDT, multithreading is possible to create in Java1.4 same as in Java7

  • in Java7 some of Thread-safe methods isn't Thread-safe in compare with Java6 on WinXP for MetalLookAndFeel

mKorbel
  • 109,525
  • 20
  • 134
  • 319