Java invokeAndWait?

∥☆過路亽.° 提交于 2019-12-11 06:34:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!