V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Weixiao0725
V2EX  ›  Java

Java 多线程中的 synchronization order 如何理解为全序?

  •  
  •   Weixiao0725 · 2017-06-15 22:35:50 +08:00 · 3060 次点击
    这是一个创建于 2499 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在看 Java 多线程中的一些抽象概念,其中涉及到一个概念是 synchronization order,JSL 17.4.14中的表述为"Every execution has a synchronization order. A synchronization order is a total order over all of the synchronization actions of an execution."这里为什么是对所有的同步操作是全序呢?比如一个在 monitor m 上的 unlock 操作肯定是 happen-before 在该 monitor m上的 lock 操作,这个是有序的,但是他与一个 lock on monitor n 的操作是没有关系的啊,也不会在 order 上有比较吧?看了很多资料,都是这么描述的,但是实在没有理解这里的说法,到底是我哪里理解有偏差吗?多谢大家!

    11 条回复    2017-06-16 12:19:51 +08:00
    hwding
        1
    hwding  
       2017-06-15 22:52:13 +08:00
    https://stackoverflow.com/questions/44565995/how-to-understand-the-synchronization-order-which-is-a-total-order
    这个也是你问的吗...

    “ An unlock action on monitor m synchronizes-with all subsequent lock actions on m (where "subsequent" is defined according to the synchronization order). ”
    这个不就是说的只针对 monitor m 吗?
    Weixiao0725
        2
    Weixiao0725  
    OP
       2017-06-15 23:01:37 +08:00
    是啊,我被这个问题困扰一整个星期了,我在 v2,stackoverflow,zhihu 上都提问了,还去 irc 的 java 频道去提问,真的希望可以得到一个正确的观点。
    Weixiao0725
        3
    Weixiao0725  
    OP
       2017-06-15 23:03:27 +08:00
    @hwding , 我理解的肯定也是只针对 Monitor m,但是如果这样的话,定义中的那个 total order over ALL synchronization actions 又怎么理解呢?难道 ALL 是说对同一个 monitor 的 ALL 吗?
    hwding
        4
    hwding  
       2017-06-15 23:17:58 +08:00
    @Weixiao0725
    你这么一说我也糊涂了,原来自己理解的并没有那么透彻 :(
    不过局限范围好像是 an execution,对 all sync actions 有全序。

    我找了两篇论文,你要是先看懂了教我下啊 :)

    http://www.cs.umd.edu/~pugh/java/memoryModel/CommunityReview.pdf

    http://www.cs.umd.edu/~pugh/java/memoryModel/unifiedProposal/model3.pdf
    Weixiao0725
        5
    Weixiao0725  
    OP
       2017-06-15 23:24:46 +08:00
    我看了好多资料里都这么描述的 total order。一定是我哪里理解偏差了,我先看下,共勉~
    jaryur
        6
    jaryur  
       2017-06-16 01:08:17 +08:00
    of an execution 这句是指在一个线程内,另外表达的意思是同步顺序是指所有同步操作的执行总顺序,因为在一个代码块中可以包含多个同步操作,在这句后面有一条解释:For each thread t, the synchronization order of the synchronization actions (§17.4.2) in t is consistent with the program order (§17.4.3) of t.我理解的是同步代码的程序顺序和同步顺序是一致的,也就是我们说的同步代码的禁止指令重排序。之后又给出的具体 synchronized-with 规则
    SoloCompany
        7
    SoloCompany  
       2017-06-16 01:21:06 +08:00
    JLS 里面并没有说两个不同的 monitor m, n 存在定义上的互斥关系

    17.4.3 第一行定义就很清楚的指出是 monitor m 的 unlock 操作必须发生在同一个 monitor 的 lock 操作之前

    但 synchronization order 的定义表述是不能局限于某一个 monitor 上,比如死锁问题,单独局限在任一个 monitor 下都不存在的,所以序和锁是不同的抽象概念,因为有序才会发生死锁

    除了第一条,17.4.3 后面列出的所有和序相关的规则都和 monitor 无关
    比如 volatile 关键字, read 允许并发, 但 write 必须发生同一个变量的 read 之前
    又如 thread.join() 和 thread.isAlive()
    等等等
    Weixiao0725
        8
    Weixiao0725  
    OP
       2017-06-16 09:36:24 +08:00
    @jaryur of an execution 并不是只一个线程内哦,参看[这篇文章]( https://www.playingwithpointers.com/peeking-into-jmm.html), 里面有明确的描述:The second one is the synchronization order (we ’ ll denote this by so⇝) – this is a global (cross thread) total order on “ synchronization actions ”。在每个线程内,所有的操作是 total order 这个没问题,但是现在问题是为什么跨线程之间的所有 synchronizaiton actions 会构成一个 total order
    Weixiao0725
        9
    Weixiao0725  
    OP
       2017-06-16 09:51:27 +08:00
    @SoloCompany 应该说这里是用锁实现的序,但是我不明白的是为什么在不同的线程间的对不同对象的同步操作会构成全局的一个 total order 关系
    Weixiao0725
        10
    Weixiao0725  
    OP
       2017-06-16 11:33:53 +08:00
    @hwding 你看下我在 stackoverflow 问那个问题,有个人回复了,在他的回复的基础上我又看了下,大概是明白了。有问题我们再交流。
    hwding
        11
    hwding  
       2017-06-16 12:19:51 +08:00
    @Weixiao0725 OK, thx.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2759 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:43 · PVG 20:43 · LAX 05:43 · JFK 08:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.