第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

java并發(fā)問題

java并發(fā)問題

MMTTMM 2019-02-03 23:59:26
 public class Apple {    public int a = 0;    public Apple() {        for (int i = 0; i < 50000; i++) {             a = i;         }     } }public class SimpleCounter {     Apple apple;//   volatile Apple apple;     public void setApple() {         apple = new Apple();     }    public Apple getApple() {        while (true) {            if (null != apple) {                return apple;             }         }     } }@RunWith(ConcurrentTestRunner.class)public class TestSimpleCounter {    private SimpleCounter simpleCounter = new SimpleCounter();    @Test     public void testSetApple() {         simpleCounter.setApple();     }    @Test     public void testGetApple() {         System.out.println(simpleCounter.getApple().a);     } }為什么上述代碼不能停止運(yùn)行,改成volatile就可以,測試框架用的concurrent-junit。
查看完整描述

1 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

大概是內(nèi)存可見性問題

什么都不做(不volatile 不lock 等)時(shí)JVM不保證一線程能讀到其他線程的寫


查看完整回答
反對 回復(fù) 2019-02-03
  • 1 回答
  • 0 關(guān)注
  • 421 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)