what are read barriers and write barriers in synchronized block

前端 未结 4 1259
半阙折子戏
半阙折子戏 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:02

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

提交回复
热议问题