问题
This is a really simple question about the invokeAndWait thing from swing utilities. I have heard that it synchronizes code execution on a single thread, but I'm not sure. If so, should I use invokeAndWait to do that?
回答1:
SwingUtilities.invokeAndWait(Runnable)
will enqueue the Runnable
on the Event Queue. This will allow the Event Dispatching Thread to execute the run
method of the Runnable
within the context of the Event Dispatching Thread.
invokeAndWait
will not return until AFTER the EDT has finished executing the run
method. This means it's a blocking operation.
invokeAndWait
is used to re-sync code to the EDT, allowing it to execute updates to the UI within the Swing toolkit.
Unless you are trying to get you code to be executed on the EDT, no, you shouldn't use it for thread synchronization.
来源:https://stackoverflow.com/questions/15080772/java-invokeandwait