问题描述
我最近了解到,Sun的/ Oracle最新指南说,任何Swing对象(包括构造函数)的Swing方法都不能在EDT之外调用。
I recently learnt that Sun's/Oracle's most recent guidelines say that no Swing methods of any Swing objects, including constructors, must be called outside the EDT.
同样的严格标准是否适用于所有视觉AWT课程?如果没有,**对他们来说是什么**?
Does the same standard of rigour apply to all "visual" AWT classes too? If not, what ** are ** the rules for them?
以后
later
re Swing和EDT:2009年的讨论。
re Swing and EDT: discussion from 2009.
引用:
除此之外实际的线程安全性和相关问题,如可见性和
同步,我认为是软件问题。摇摆组件
经常有某种类型的监听器,而这些监听器的设计是
执行EDT。
quote:"Besides actual thread safety and associated issues like visibility andsynchronization, there's I think a software issue. Swing componentsoften have "listeners" of some type, and these listeners are designed toexecute on the EDT.
由于这些侦听器是异步的并且响应事件(例如
属性更改),因此可以让这些侦听器触发,因为
构建你的GUI。结果是,当你在主t中构建时,一些听众正在EDT上执行
hread和
一些侦听器也可能在其他一些线程上运行(因为
监听器混淆并在错误的线程上触发)。结果是
a巨大的不可预测的混乱。
Since these listeners are asynchronous and respond to events (likeproperty changes) it's possible to have these listeners fire as youconstruct your GUI. The result is that some listeners are beingexecuted on the EDT as you are constructing in your main thread, andsome listeners might be running on some other thread as well (becausethe listener is confused and fires on the wrong thread). The result isa huge unpredictable mess."
也许他们不知道他们在说什么......但是现在我正在服用一个更安全而不是抱歉的方法。也是Potochkin,在似乎认为我们熟悉后来更严格的规则
maybe they don't know what they're talking about... but at the moment I'm taking a "better safe than sorry" approach. Also Potochkin, at http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html seems to take it as read that we are familiar with the later, stricter rules
推荐答案
多线程Java程序中的正确同步取决于发生之前的关系,总结在 .AWT组件意图是线程安全的,在 java.awt.Component
中的私有锁对象上进行同步。请参阅一些历史视图的注释ve:
Correct synchronization in a multi-threaded Java program hinges on the happens-before relation, summarized in Memory Consistency Properties. AWT Components were intended to be thread safe, synchronizing on a private lock object in java.awt.Component
. See the comments for some historical perspective:
private transient Object objectLock = new Object();
虽然这可能对简单程序来说已经足够了,但是需要更复杂的程序来依赖这种实现的知识细节以验证正确的同步。这是可能的,但是谁愿意接受一个脆弱的AWT GUI?
While this may prove sufficient for simple programs, more complex programs are required to rely on knowledge of this implementation detail to verify correct synchronization. It's possible, but who wants to settle for a brittle AWT GUI?
一些额外的积分:
-
@Hovercraft引用的日期是1998年,但它已经反复更新以解决诸如在usenet中提到。
The article cited by @Hovercraft dates to 1998, but it has been updated repeatedly to address such issues as the new memory model mentioned in the usenet thread you cited.
javax.swing的演变
已远离GUI API承诺,如所述,并且更灵活。
The evolution of javax.swing
has been away from GUI API promises, as mentioned here, and toward more flexible concurrent programming tools.
这篇关于必须在EDT中调用AWT类中的所有方法,即非Swing方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!