Coder Social home page Coder Social logo

Comments (4)

CoDeleven avatar CoDeleven commented on June 11, 2024

可以保证。可以从两个方面理解:
内存屏障:
volatile关键字会在volatile写之前插入StoreStore,保证volatile写之前的内容先写入主内存,保证对所有线程可见(禁止a写和flag写的重排序);而volatile读后会插入LoadLoad和LoadStore,保证在读取变量a前,先读取flag(禁止a读和flag读重排序)。所以就能保证它们的可见性。

happens-before:
作者的代码没看,你可以看我的理解一下 /捂脸
image

最后根据传递性规则,① happens-before ④

from java-concurrency.

MikasaLee avatar MikasaLee commented on June 11, 2024

懂了,谢谢老哥指点

from java-concurrency.

MikasaLee avatar MikasaLee commented on June 11, 2024

老哥还想在问一下细节
JMM的内存屏障只是用来确保对volatile对象的读写操作不会发生重排序。
但是真正volatile关键字真正用来同步线程内存和主内存的操作是原文说的"Lock前缀的指令"这个作用吧,这个理解对么?
(顺便问一下为啥我javap 看指令操作时没有发现一个Lock开头的指令。。)

from java-concurrency.

CoDeleven avatar CoDeleven commented on June 11, 2024

lock开头的指令是在JIT的汇编代码里
参考周志明的《深入Java虚拟机》P111

from java-concurrency.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.