Is happens-before relation given in case of invokeLater() or invokeAndWait?

人盡茶涼 提交于 2019-12-12 15:21:24

问题


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?*

回答1:


In short: yes, there is a happens-before relationship imposed between actions of the thread calling invokeLater/invokeAndWait and actions on the EDT of the runnable thereby submitted. Without that the sanity of the whole API would be at stake.

Unfortunately, it is hard to come by any authoritative source which would confirm that. That happens with a lot of stuff regarding Swing and concurrency.

For a bit more information, refer to this answer by trashgod, a long-time Swing guru.



来源:https://stackoverflow.com/questions/21147240/is-happens-before-relation-given-in-case-of-invokelater-or-invokeandwait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!