简体   繁体   English

同步块中的读取障碍和写入障碍是什么

[英]what are read barriers and write barriers in synchronized block

I am looking into how a synchronized and volatile variable works in java and i came across a concept called read and write barrier . 我正在研究同步和volatile变量如何在java中工作,我遇到了一个名为read and write barrier的概念。 Can anyone help me to understand the meaning of this terms 任何人都可以帮助我理解这个术语的含义

( the answers above are quite complete), I just want to demonstrate the concept with a simple scheme (上面的答案非常完整),我只想用一个简单的方案来演示这个概念

 Thread 1                                                             Thread 2


   |          
   |                                                                      
   |                                                                      |
   |                                                                      |
   |  Everything Thread 1                                                 |
   |   wrote before here                                                  |
   |                                                                      |
   |                                                                      |
    _ _ _ _ _ _ _ _ _ _                                                   |
      ( write  barrier)         (happens before)         (read barrier)   |
   |                                                     _ _  _ _ _ _ _ _  
   |
   |                                                   is guaranteed      |
   |                                                  to be visible to    |
   |                                                  Thread 2            |
   |                                                                      |

A memory barrier is a conceptual "line" in your code that prevents the compiler from making certain optimizations and may insert special "sync up" commands to the processor. 内存屏障是代码中的概念“线”,它阻止编译器进行某些优化,并可能向处理器插入特殊的“同步”命令。 Normally, the compiler can look within a particular method and see that certain instructions can be moved around without changing the meaning of the code. 通常,编译器可以查看特定方法,并查看某些指令可以在不改变代码含义的情况下移动。 For example, if you have 例如,如果你有

int x = 0, y = 0;
x++;
y++;

If the compiler figured that there was some benefit, it could instead output code for 如果编译器认为有一些好处,它可以代替输出代码

y++;
x++;

However, if x and y are fields in some class, so that they can be seen from other threads, the other thread might be modifying the values while your method is running. 但是,如果xy是某个类中的字段,以便可以从其他线程中看到它们,则另一个线程可能在您的方法运行时修改这些值。

A memory barrier forces the compiler to recheck the values of specific variables (in Java, those are ones that are volatile and the Atomic* classes) in case some other thread has modified them while the method's been running, and it keeps the compiler from making reorderings that might accidentally change the results of a calculation. 内存屏障迫使编译器重新检查特定变量的值(在Java中,那些是volatileAtomic*类),以防某些其他线程在方法运行时修改它们,并且它使编译器无法生成重新排序可能会意外更改计算结果。 On systems that support multiple cores/processors, the compiler will also force the processor to check to make sure that some other processor or hardware device hasn't modified the variable in the meantime. 在支持多核/处理器的系统上,编译器还会强制处理器检查以确保其他处理器或硬件设备在此期间未修改变量。 Java (as of Java 5) has an extremely well-defined set of rules for how this works called happens-before . Java(从Java 5开始)有一套非常明确的规则,用于说明这种工作如何发生

This FAQ has some useful explanations that were written at the time the Java Memory Model was being developed. 这个FAQ有一些有用的解释,这些解释是在Java Memory Model开发时编写的。 Note that while the concept of a memory barrier is cross-language, most languages don't have as clearly defined rules about them as Java. 请注意,虽然内存屏障的概念是跨语言的,但大多数语言没有像Java那样明确定义它们的规则。

When you enter a synchronized block of code you pass the "read barrier" and when it is exited you pass the "write barrier". 当您输入同步的代码块时,您将传递“读取屏障”,当它退出时,您将通过“写屏障”。

Is is used in reference to volatile attributes, and give an indication when Threads need to update their values of the volatile attributes. Is用于引用volatile属性,并在Threads需要更新volatile属性的值时给出指示。 They should update it when they are passing the read barrier if anyone else passed the write barrier. 如果其他人通过写屏障,他们应该在通过读屏障时更新它。

Similar reading from a volatile attribute makes yout thread pass the read barrier and writing to a volatile attribute makes you pass the write barrier, hence much more fine grained than a synchronized block. 类似于从volatile属性读取使得线程通过读屏障并且写入volatile属性会使您通过写屏障,因此比同步块更精细。

Read and write barriers are used to implement the semantics of the Java Memory Model at the lowest level by JVMs. 读写障碍用于通过JVM在最低级别实现Java内存模型的语义。

However that terminology is absent from the Java Language Specification, which only reasons in terms of happens-before relationships. 但是,Java语言规范中没有这个术语,这只能说明之前发生的关系。 In particular 特别是

  • a write to a volatile variable happens-before a subsequent read of that same variable 在对同一变量的后续读取之前发生对易失性变量的写入
  • exiting a synchronized block happens-before a subsequent entry of that same syncchronized block 退出同步块发生在相同的同步块的后续条目之前

When a happens-before relationship exists between two actions in your program, you have the guarantee that those two actions will be executed in a sequentially consistent order (ie as if there were only one thread and without unintuitive reorderings). 当程序中的两个操作之间存在一个before-before关系时,您可以保证这两个操作将按顺序一致的顺序执行(即好像只有一个线程并且没有不直观的重新排序)。

Delving into the implementation details of the JVMs is unnecessary to write a correct multi-threaded program. 编写正确的多线程程序不需要深入研究JVM的实现细节。 However, if you want the gory details, the JSR-133 cookbook is an interesting read. 但是,如果你想要血淋淋的细节, JSR-133食谱是一本有趣的读物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM