本文介绍了在invokeLater()或invokeAndWait情况下是否给出事前发生关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以肯定是这样-但我想确定-在invokeLater()或invokeAndWait()的情况下,是发生在事前关系的吗?

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

方法在(分别为SwingUtilities)AWT.EventQueue中定义.我猜想在EventQueue中输入内容时会涉及到同步因此,作为同步的结果,给出了事前发生的关系并最终给出了可见性.

The methods are defined in (SwingUtilities respectively) AWT.EventQueue.I guess there is synchronization involved when something is entered in the EventQueueand 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?*

推荐答案

简而言之:是的,在调用invokeLater/invokeAndWait的线程的动作之间强加了先发关系.以及由此提交的可运行文件的EDT操作.否则,整个API的合理性将受到威胁.

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.

不幸的是,很难有任何权威来源能够证实这一点.发生了很多有关Swing和并发的事情.

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.

有关更多信息,请参考 trashgod 中的此答案,时间摇摆大师.

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

这篇关于在invokeLater()或invokeAndWait情况下是否给出事前发生关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:48
查看更多