问题描述
为什么有必要将GUI更新代码放在 SwingUtilities.invokeLater()
?
Why is it necessary to put GUI update code in SwingUtilities.invokeLater()
?
为什么不能由Swing自己内部照顾?为什么调用者必须关心swing如何处理UI更新?
Why cant it be internally taken care of by Swing itself? Why does the caller have to care about how swing handles UI updates?
推荐答案
Swing对象。 SwingUtilities.invokeLater()
允许在稍后的某个时间点执行任务,顾名思义;但更重要的是,该任务将在AWT事件派发线程上执行。使用 invokeLater
时,任务以异步方式执行;还有 invokeAndWait
,在任务完成执行之前不会返回。
Swing objects are not thread safe. SwingUtilities.invokeLater()
allows a task to be executed at some later point in time, as the name suggests; but more importantly, the task will be executed on the AWT event dispatch thread. When using invokeLater
, the task is executed asynchronously; there's also invokeAndWait
, which won't return until the task has finished executing.
有关决定的一些信息没有可以在这里找到使Swing线程安全的方法:
Some information about the decision not to make Swing thread-safe can be found here: Multithreaded toolkits: A failed dream?
这篇关于SwingUtilities.invokeLater()为什么需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!