SwingUtilities.invokeLater() 为什么需要它? [英] SwingUtilities.invokeLater() why is it needed?

查看:34
本文介绍了SwingUtilities.invokeLater() 为什么需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要在SwingUtilities.invokeLater()中放置GUI更新代码?

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? [Archived]

这篇关于SwingUtilities.invokeLater() 为什么需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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