what are read barriers and write barriers in synchronized block

前端 未结 4 1270
半阙折子戏
半阙折子戏 2021-01-12 04:44

I am looking into how a synchronized and volatile variable works in java and i came across a concept called read and write barrier . Can anyone help me to understand the mea

4条回答
  •  感动是毒
    2021-01-12 05:12

    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. 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.

提交回复
热议问题