为什么 Swing 不是“线程安全的"? [英] Why is not Swing "thread safe"?

查看:44
本文介绍了为什么 Swing 不是“线程安全的"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有关线程的 Java 课程的最后几分钟,我们的教授建议我们在开发基于 Swing 的复杂应用程序时要特别注意,因为 Swing 不是线程安全的.这背后有什么具体原因吗?Swing 由于设计决定或软件限制而不是线程安全的?提前感谢您的回答.

In the final minutes of a Java lesson about threads, our professor suggested us to pay specific attention when developing complex Swing-based applications, since Swing is not thread-safe. Is there any specific reason behind this? Swing is not thread safe due to a design decision, or due to software limitations? Thanks in advance for the answer.

推荐答案

Javin Poul 的编程博客 Java Revisiteda很棒的博文关于这个:

Javin Poul's programming blog Java Revisited has a great blog post about this:

为什么 Swing 在 Java 中不是线程安全的?

这是当时设计师做出的决定.由于使 API 线程安全需要大量工作,而且通常取决于您获得的收益.由于 GUI 屏幕大多是响应用户操作而更新的,例如当用户单击按钮时,由于事件是在同一个事件调度程序线程中处理的,因此很容易在该线程上更新 GUI.这是非常罕见的,当 GUI 的更新请求来自不同的线程时,例如可能是在网络请求完成或文件加载后.

It's the decision taken by there designer, at that time. Since making an API thread-safe takes a lot of work, and it's often based upon benefit you get. Since GUI screens are mostly updated in response of user action e.g. when user click a button, and since events are handled in the same Event dispatcher thread, it's easy to update GUI on that thread. It's very rare, when an update request for GUI comes from a different thread e.g. may be once a network request is complete or a file is loaded.

这篇关于为什么 Swing 不是“线程安全的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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