關(guān)于JMM(Java Memory Model)

What is a memory model?

At the processor level, a memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are visible to the current processor, and writes by the current processor are visible to other processors.

What is the JMM?

The Java Memory Model describes what behaviors are legal in multithreaded code, and how threads may interact through memory. 
It describes the relationship between variables in a program and the low-level details of storing and retrieving them to and from memory or registers in a real computer system. 
It does this in a way that can be implemented correctly using a wide variety of hardware and a wide variety of compiler optimizations.

What does synchronization do?

  • mutual exclusion -- only one thread can hold a monitor at once
  • ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor.

Happen before

When one action happens before another, the first is guaranteed to be ordered before and visible to the second

The rules of this ordering are as follows:

  • Each action in a thread happens before every action in that thread that comes later in the program's order.
  • An unlock on a monitor happens before every subsequent lock on that same monitor.
  • A write to a volatile field happens before every subsequent read of that same volatile.
  • A call to start() on a thread happens before any actions in the started thread.
  • All actions in a thread happen before any other thread successfully returns from a join() on that thread.

What does volatile do?

  • ensure that after fields are written, they are flushed out of the cache to main memory
  • before a volatile field is read, the cache must be invalidated so that the value in main memory, not the local processor cache, is the one seen.
    下面是一個(gè)volatile的用法
class VolatileExample {
      int x = 0;
      volatile boolean v = false;
      public void writer() {
        x = 42;
        v = true;
      }

      public void reader() {
        if (v == true) {
          //uses x - guaranteed to see 42.
        }
    }
}

也許有人會(huì)問(wèn)狮崩,x并沒(méi)有volatitle修飾辞居,為什么也保證了可見(jiàn)性堂湖?解釋如下:

The write to v in writer releases the write to x to memory, and the read of v acquires that value from memory. 
Volatile or not, anything that was visible to thread A when it writes to volatile field f becomes visible to thread B when it reads f.

小弟在上一篇文章中提到過(guò)镐侯,volatile相當(dāng)于在指令前添加了LOCK指令(內(nèi)存屏障),即:

  • 內(nèi)存中在寫(xiě)volatile后插入了write-barrier,release工作內(nèi)存中數(shù)據(jù)到主存抖誉;
  • 讀volatile之前插入了read-barrier估脆,將主存數(shù)據(jù)刷新到工作內(nèi)存钦奋。

引用

淺析java內(nèi)存模型--JMM(Java Memory Model)
JSR 133 (Java Memory Model) FAQ
深入理解Java內(nèi)存模型(一)——基礎(chǔ)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市疙赠,隨后出現(xiàn)的幾起案子付材,更是在濱河造成了極大的恐慌,老刑警劉巖圃阳,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厌衔,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡捍岳,警方通過(guò)查閱死者的電腦和手機(jī)富寿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)睬隶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人页徐,你說(shuō)我怎么就攤上這事苏潜。” “怎么了变勇?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵窖贤,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我贰锁,道長(zhǎng),這世上最難降的妖魔是什么滤蝠? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任豌熄,我火速辦了婚禮,結(jié)果婚禮上物咳,老公的妹妹穿的比我還像新娘锣险。我一直安慰自己,他們只是感情好览闰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布芯肤。 她就那樣靜靜地躺著,像睡著了一般压鉴。 火紅的嫁衣襯著肌膚如雪崖咨。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天油吭,我揣著相機(jī)與錄音击蹲,去河邊找鬼。 笑死婉宰,一個(gè)胖子當(dāng)著我的面吹牛歌豺,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播心包,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼类咧,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蟹腾?” 一聲冷哼從身側(cè)響起痕惋,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎岭佳,沒(méi)想到半個(gè)月后血巍,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡珊随,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年述寡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了柿隙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鲫凶,死狀恐怖禀崖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情螟炫,我是刑警寧澤波附,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站昼钻,受9級(jí)特大地震影響掸屡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜然评,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一仅财、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧碗淌,春花似錦盏求、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至纳像,卻和暖如春荆烈,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背爹耗。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工耙考, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人潭兽。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像山卦,于是被迫代替她去往敵國(guó)和親鞋邑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容