1

Quote form the Swing Tutorial

Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread.

And on the same page some lines below

If you need to determine whether your code is running on the event dispatch thread, invoke javax.swing.SwingUtilities.isEventDispatchThread.

So thread-safe methods like SwingUtilities.invokeLater are "labeled" with a text like this:

Unlike the rest of Swing, this method can be invoked from any thread.

But the API documentation for SwingUtilities.isEventDispatchThread() does not "label it" as thread-safe.

Returns true if the current thread is an AWT event dispatching thread.

This would mean that I may call it only on the event dispatch thread. But that doesn't make any sense, so I'm pretty sure the method is thread-safe.

Right?

Is there any confirmation for this on the web?

niks
  • 2,316
  • 1
  • 15
  • 15
  • true/false as returns from isEventDispatchThread notify about if is EDT active (returns is true), otherwise (EDT queue is empty plus some timeout) returns false, I think that there isn't required some thread safety in API, is just a notifier about the status – mKorbel Nov 25 '15 at 10:11
  • No, it wouldn't mean that you 'may only call it on the event despatch thread'. Where did you get that idea? If you do, it returns true, otherwise false. Is the fact that it is or isn't the dispatcher thread really likely to be thread-unsafe? It's only one bit of information, and it's true for one thread and false for all others. Not exactly volatile data. – user207421 Nov 25 '15 at 10:13
  • Two other approaches for finding violations are cited [here](http://stackoverflow.com/q/7787998/230513). – trashgod Nov 25 '15 at 11:39
  • Note that `EventQueue.isDispatchThread()` is _not_ a "Swing component method." – trashgod Nov 25 '15 at 11:45
  • 1
    Well, the question then probably is: What _is_ a "Swing component method". I assumed every method on every class in a `javax.swing.*` package, including `SwingUtilities`. Otherwise it wouldn't be necessary to label `SwingUtilities.invokeLater` as thread-safe. So if `SwingUtilities`is a "Swing component" then its `isEventDispatchThread()` method should be labeled as thread-safe, if it is thread-safe. – niks Nov 26 '15 at 08:18

0 Answers0