2-4節(jié),為什么我的實(shí)際輸出始終為20000
class?Req1?implements?Runnable{ ???static?Req1?req1?=?new?Req1(); ???static?int?i=0; ???@Override ???public?void?run()?{ ??????for(int?j?=?0;j<10000;j++){ ?????????i++; ??????} ???} ???public?static?void?main(String[]?args)?throws?InterruptedException?{ ??????Thread?thread1?=?new?Thread(req1); ??????Thread?thread2?=?new?Thread(req1); ??????thread1.start(); ??????thread2.start(); ??????thread1.join(); ??????thread2.join(); ??????System.out.println(i); ???} }
2019-05-14
循環(huán)次數(shù)太少了,CPU瞬間執(zhí)行完,兩個(gè)線程沒有交叉執(zhí)行,或者交叉次數(shù)太少且沒有出現(xiàn)i++被中斷執(zhí)行的情況