JLabel setText()方法被认为是线程安全的,但它能确保可见性吗



查看JLabel的源代码时,我担心文本字段的可见性。我提取了关键部分来设置文本字段并检索oldValue。在我看来,如果String文本字段没有被声明为volatile,可见性就会受到威胁,因为firePropertyChange可能看不到从另一个线程之前保存的文本字段中检索到的oldValue。我是对的,还是我错过了什么?请注意,这不是为了讨论SwingUtility。

public class JLabel extends JComponent implements SwingConstants, Accessible {
    ...
    private String text = "";         // "" rather than null, for BeanBox
    ...
    public void setText(String text) {
        String oldAccessibleName = null;
        if (accessibleContext != null) {
            oldAccessibleName = accessibleContext.getAccessibleName();
        }
        String oldValue = this.text;
        this.text = text;
        firePropertyChange("text", oldValue, text);
    ...

非常感谢。

根据您所写的内容,不,这不能确保可见性(尽管我不认为声明任何volatile会有什么帮助)。在这个bug中,JDK 6文档中的许多方法错误地声明它们是线程安全的(请参见页面底部),JEditorPane.setText就是其中之一(因此JDK 7文档中没有该声明)。