Friday, April 15, 2011

Is Component.getGraphicsConfiguration thread safe?

There are many methods you shouldn't call if you are not on the AWT event thread. These are generally methods that manipulate the UI in some way.

Is this the case with Component's getGraphicsConfiguration(...)? It is only a getter but it appears to cause a deadlock if the event thread is waiting on the thread calling this method.

Whilst solving the deadlock is fairly trivial (avoid using wait or synchronize on the event thread), should I only be calling getGraphicsConfiguration in a Runnable passed to SwingUtilities.invokeLater(...) or invokeAndWait(...)?

From stackoverflow
  • AWT thread-safety is (necessarily) broken. Just don't go anywhere near it. Stay on the straight AWT EDT

    Also note that just because a method is in java.awt.Component, it does not mean that the object (or an object referenced by it) is not in Swing and therefore really not even pretending to be thread-safe at all. (Enough nots?)

    Tom Martin : er ... so you're saying yes only use the method when I'm on the event thread?
    Tom Hawtin - tackline : I am saying that it would be highly advisable to do so.

0 comments:

Post a Comment