哪位大神再給我解讀一下這段程序,謝謝
try?{
//條件不滿足,?將當(dāng)前線程放入Wait?Set
lockObj.wait();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
?}
?
?
?System.out.print(Thread.currentThread().getName());
?energyBoxes[from]?-=?amount;
?System.out.printf("從%d轉(zhuǎn)移%10.2f單位能量到%d",?from,?amount,?to);
?energyBoxes[to]?+=?amount;
?System.out.printf("?能量總和:%10.2f%n",?getTotalEnergies());
//喚醒所有在lockObj對象上等待的線程
?lockObj.notifyAll();
2016-11-01
條件不滿足時也就是其它線程還沒有將數(shù)據(jù)寫回(這里的lockObj是EnergySystem的final成員,用來標(biāo)記EnergySystem,lockObj.wait()也就相當(dāng)于EnergySystem實(shí)例.wait()),等待寫入之后(也就是滿足條件),執(zhí)行邏輯代碼,在喚醒其它線程(有可能還沒有寫入數(shù)據(jù)的)。可以回頭看爭用條件那節(jié),有助于理解,我專門截了圖。