• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is JOptionPane thread safe?  RSS feed

 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was just wondering if anybody knows. Is it safe to use the showXxxDialog methods of JOptionPane in a thread other than the EDT?

The API states that
All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete.


Also, I have found this. The bug effectively states that JOptionPane does not block the EDT. Putting this together, I can only conclude that JOptionPane can be displayed from any thread, but it blocks the thread if it is not the EDT (which is fine by me).

What do you think? Even if the answer is "it is thread safe", can I depend on it on other JVMs, like IBM's? How "global" is javadoc? Is it only for Sun's JDK, or is it how each implementation should work?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the bug is shown as being "fixed." Nowhere does it say that anything in Swing is thread safe. See this web page.
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about this:
All dialogs are modal. Each showXxxDialog method blocks the
current thread until the user's interaction is complete.

However, if the current thread is the event dispatch thread, then it is
not blocked but sent to keep pumping events until the dialog is closed.

I took this as "it does not block the EDT, but when I tried to call it from other threads, those were".

Of course, you are right that this does not mean that it is thread-safe. And indeed it is not: I took a look at the code, and nowhere does it call invokeLater/Wait, or check which thread it is running on.

But the second question still remains: if I read something in javadoc (where there is no might/may), can I expect it to work the same way on all JDKs? How "trustworthy" is the JDK in implementation details?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There have been slight changes in the API documentation for JOptionPane in Java 6, but it still says it blocks the calling thread, and is not thread-safe. I can't speak about other JVMs, but I presume (since Java is a trademark) they are obliged to conform to the behaviour in the JLS and JVM specification before they can be distributed in public.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!