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

1

8

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?*
Asphyxiate answered 15/1, 2014 at 20:11 Comment(5)
I really don't understand what you are trying to ask.Hysterogenic
In the first example in some thread at position <1> data is changed and then invokeLater() is called. Is the changed data visible at position <2> inside the runnable.Asphyxiate
invokeAndWait must be called out of EDT, tested by isEventDispatchThread must to returns false, otherwise a few exception can freeze Swing GUI, can be forever depends ofCramfull
n the first example in some thread at position <1> data is changed and then invokeLater() is called. Is the changed data visible at position <2> inside the runnable. In the second example again at position <1> data is changed and later (outside of the runnable) when invokeAndWait() has finished it is accessed at position <2>. Is the changed data visible again?Asphyxiate
Thanks to "mKorbel" for the response. Looking at "JAVA - Concurrency in practice" from Brain Goetz at all you need the synchronize to guarantee the happens-before relation and as result of that you gain visibility of data. I can't find any information that invokeLater() guarantees happens-before or visibility of data.Asphyxiate
R
5

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.

Remiss answered 15/1, 2014 at 20:40 Comment(3)
@MarkoTopolnik: Thanks! I guess, I have to accept that. Since the AWT EventQueue guarantees FIFO order when tasks/Runnables are pushed into the EventQueue by invokeLater(), there has to be synchronization. As a result of this we can assume that the “call of invokeLater()” happens-before the “start of the task” and visibility is given (in this direction). The task sees at least the values at the time when invokeLater() was called. Anyway, I would have preferred not to be required to consider about internal implementations, but to have an authoritative source (e.g. API) guaranteeing me that.Asphyxiate
I think that this behavior does not necessarily have to be documented. See https://mcmap.net/q/1471199/-java-happens-before-relationship-invokeandwait/3882565.Magnesite
In my opinion it is a mind-boggling oversight that the documentation fails to explicitly state that the invoke... methods establish a happens-before relationship. Exchanging the data between EDT and other threads is far from being an edge case scenarioJaqitsch

© 2022 - 2024 — McMap. All rights reserved.