6

Pretty sure it is this way - but I like to know for sure - is happens-before relation given in case of invokeLater() or invokeAndWait()?

The methods are defined in (SwingUtilities respectively) AWT.EventQueue. I guess there is synchronization involved when something is entered in the EventQueue and hence as result of the synchronization, the happens-before relation and finally the visibility is given.

But is it really that way? (and where can I find that information?)


e.g. inside some worker thread

    ...
    *<1> heavy computation modifying lots of DATA*
    ...
    SwingUtilities.invokeLater(
        new Runnable() {
            @Override
            public void run() {
                *<2> is visibility of modified DATA guaranteed?*
            }
        }
    );

e.g. inside some thread

    ...
    SwingUtilities.invokeAndWait(
        new Runnable() {
            @Override
            public void run() {
                ...
                *<1> change some DATA*
            }
        }
    );
    *<2> is visibility of modified DATA guaranteed?*
4

1 回答 1

3

简而言之:是的,线程调用/的动作和由此提交的可运行对象的 EDT 上的动作之间存在发生之前的关系。否则,整个 API 的健全性将受到威胁。invokeLaterinvokeAndWait

不幸的是,很难得到任何权威来源来证实这一点。很多关于 Swing 和并发的东西都会发生这种情况。

有关更多信息,请参阅资深Swing 大师垃圾神( trashgod ) 的此答案。

于 2014-01-15T20:40:51.077 回答