Java invokeAndWait?

2019-08-15 00:24发布

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条回答
孤傲高冷的网名
2楼-- · 2019-08-15 01:02

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.

查看更多
登录 后发表回答