简体   繁体   English

C#和Java中的volatile有什么区别?

[英]What are the differences between the volatile in c# and java?

In some .net documentation I have read this is how the compiler handles volatile: 在一些.net文档中,我读到了编译器如何处理volatile的方法:

  • *"Reading from a volatile or using the Thread.VolatileRead method is logically an acquire fence" *“从易失性或使用Thread.VolatileRead方法读取在逻辑上是获取隔离区”
  • "Writing to a volatile or using the Thread.VolatileWrite method is logically a release fence" * “从逻辑上讲,写到volatile或使用Thread.VolatileWrite方法都是释放屏障” *

These fences apply at both complier and architecture level. 这些围栏适用于编译器级别和体系结构级别。

Of course the main difference in VC++ is that the fence is only applied at complier level. 当然,VC ++的主要区别是,防护仅在编译器级别应用。

So my question is, what are the memory reordering prevention semantics of volatile in Java? 所以我的问题是,Java中volatile的防止内存重新排序的语义是什么?

Conversion: 转换:

Fence = Barrier Barrier = Fence 栅栏=栅栏栅栏=栅栏

References: 参考文献:

Joe Duffy (Concurrent Programming on Windows) Joe Duffy(Windows上的并发编程)

In Java, the javac compiler does next to nothing with volatile. 在Java中, javac编译器javac不使用volatile。 It doesn't re-order statements and does almost no optimisation. 它不会重新排序语句,并且几乎不会进行优化。

The JIT on the other hand can do quite a bit of optimisation and re-ordering. 另一方面,JIT可以进行很多优化和重新排序。

The important features of volatile are; volatile的重要特征是:

  • read/write access cannot be optimise away 无法优化读取/写入访问权限
  • any write which occurs before a volatile write has to occur before the write. 在易失性写之前发生的任何写都必须在写之前发生。
  • any read which occurs after a volatile read, must occur after the read. 易失性读取后发生的任何读取都必须在读取后发生。

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

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