且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:Swing 库和线程安全

更新时间:2023-01-13 17:52:18

  1. 永远不要为响应按钮、事件等而执行长时间运行的任务,因为这些都在事件线程上.如果您阻止事件线程,整个 GUI 将完全没有响应,从而导致用户非常生气.这就是为什么 Swing 看起来又慢又硬的原因.

  1. Never do long running tasks in response to a button, event, etc as these are on the event thread. If you block the event thread, the ENTIRE GUI will be completely unresponsive resulting in REALLY pissed off users. This is why Swing seems slow and crusty.

使用线程、执行器和 SwingWorker 来运行不在 EDT(事件调度线程)上的任务.

Use Threads, Executors, and SwingWorker to run tasks NOT ON THE EDT ( event dispatch thread).

不要在 EDT 之外更新或创建小部件.您可以在 EDT 之外执行的唯一调用是 Component.repaint().使用 SwingUtilitis.invokeLater 确保某些代码在 EDT 上执行.

Do not update or create widgets outside of the EDT. Just about the only call you can do outside of the EDT is Component.repaint(). Use SwingUtilitis.invokeLater to ensure certain code executes on the EDT.

使用 EDT 调试技术 和智能的外观和感觉(如 Substance,它检查 EDT 违规)

Use EDT Debug Techniques and a smart look and feel (like Substance, which checks for EDT violation)

如果你遵循这些规则,Swing 可以制作一些非常有吸引力和响应式的 GUI

If you follow these rules, Swing can make some very attractive and RESPONSIVE GUIs

一些非常棒的 Swing UI 工作示例:Palantir Technologies.注意:我不为他们工作,只是一个很棒的挥杆的例子.可惜没有公开演示......他们的博客也很好,稀疏,但很好

An example of some REALLY awesome Swing UI work: Palantir Technologies. Note: I DO NOT work for them, just an example of awesome swing. Shame no public demo... Their blog is good too, sparse, but good