unlike SwingUtilities.invokeAn开发者_开发百科dWait(), the event thread is permitted to call SwingUtilities.invokeLater()
. I cannot understand this clearly.
Please help me in this.
Another thread (not the main Swing event thread) can call invokeAndWait to wait for the Swing event thread to be ready to run some action. However, the Swing event thread cannot wait for itself. So it calls invokeLater to put the action in a queue for later execution.
Not being a Java developer, I think that if invokAndWait
is executed on an event thread, it will effectively block itself.
Source
The invokeAndWait() method looks similar, but it has three important semantic differences. First, the invokeLater() method runs asynchronously at some time in the future. You don't know when it will actually run. On the other hand, the invokeAndWait() method is synchronous: it does not return until its target has completed execution. As a rule of thumb, then, you should use the invokeAndWait() method to read the value of Swing components or to ensure that something is displayed on the screen before you continue program execution. Otherwise, you can use the invokeLater() method.
The second difference is that the invokeAndWait() method cannot itself be called from the event-dispatching thread. The thread running the invokeAndWait() method must wait for the event-dispatching thread to execute some code. No thread, including the event-dispatching thread, can wait for itself to do something else. Consequently, if you execute the invokeAndWait() method from the event-dispatching thread, it throws a java.lang.Error. That causes the event-dispatching thread to exit (unless you've taken the unusual step of catching Error objects in your code); in turn, your entire program becomes disabled.
The third difference is that the invokeAndWait() method can throw an InterruptedException if the thread is interrupted before the event-dispatching thread runs the target, or an InvocationTargetException if the Runnable object throws a runtime exception or error.
-IvarD
精彩评论