很多时候我在想为什么 Java
强制我们使用 EDT在使用 Swing
时?
从任何 Thread
更新 Swing
组件会不会更容易,或者换句话说,它可以是 多线程
。
确实,SwingUtilities.invokeAndWait
和 SwingUtilities.invokeLater
帮助我们
排列 Swing
事件,但是,如果 GUI
它们不会进入画面
组件可以从任何其他 Thread
更新。
而且,Java 制造商真的忘记了让 Swing
成为线程安全的吗?
swing如果是多线程会出现什么问题?
由于这个原因,我的 Java 程序“卡住”了十几次:/
What problem may arise if swing is Multithreaded? Have Java makers really forgotten to make Swing Thread-Safe?
已经进行了许多尝试来创建多线程 GUI,但是 most have failed要么是因为它们不起作用,要么是因为它们过于复杂而无法正确使用。单线程模型是标准且最简单的解决方案。
My Java program have "Frozen" a dozen of time beacuse of this
那是因为您没有按照应有的方式使用 Swing。它是单线程的事实并不能阻止您在另一个线程上运行长时间的任务(并且您应该这样做以防止卡住)。您可以在 Concurrency in Swing Tutorial 中阅读更多相关信息.特别是:
Careful use of concurrency is particularly important to the Swing programmer. A well-written Swing program uses concurrency to create a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing. To create a responsive program, the programmer must learn how the Swing framework employs threads.