Java invokeAndWait? [英] Java invokeAndWait?

查看:27
本文介绍了Java invokeAndWait?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,关于来自 Swing 实用程序的 invokeAndWait 问题.我听说它在单个线程上同步代码执行,但我不确定.如果是这样,我应该使用 invokeAndWait 来做到这一点吗?

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?

推荐答案

SwingUtilities.invokeAndWait(Runnable) 将把 Runnable 加入事件队列.这将允许事件调度线程在事件调度线程的上下文中执行 Runnablerun 方法.

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 直到 EDT 执行完 run 方法后才会返回.这意味着这是一个阻塞操作.

invokeAndWait will not return until AFTER the EDT has finished executing the run method. This means it's a blocking operation.

invokeAndWait 用于将代码重新同步到 EDT,允许它执行对 Swing 工具包内的 UI 的更新.

invokeAndWait is used to re-sync code to the EDT, allowing it to execute updates to the UI within the Swing toolkit.

除非您试图让代码在 EDT 上执行,否则您不应将其用于线程同步.

Unless you are trying to get you code to be executed on the EDT, no, you shouldn't use it for thread synchronization.

这篇关于Java invokeAndWait?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆