您好,登錄后才能下訂單哦!
- public class Payment
- {
- public int sku = 100;
- public static void main(String[] args)
- {
- Payment p = new Payment();
- Order1 o1 = new Order1(p);
- Order2 o2 = new Order2(p);
- o1.start();
- o2.start();
- }
- }
- class Order1 extends Thread
- {
- Payment p;
- public Order1(Payment p)
- {
- this.p = p;
- }
- @Override
- public void run()//重寫(xiě)run()
- {
- synchronized(p) //對(duì)象p加鎖
- {
- p.sku -= 1;
- System.out.println("剩余庫(kù)存:" + p.sku);
- }
- }
- }
- class Order2 extends Thread
- {
- Payment p;
- public Order2(Payment p)
- {
- this.p = p;
- }
- @Override
- public void run()
- {
- synchronized(p)
- {
- p.sku -= 1;
- System.out.println("剩余庫(kù)存:" + p.sku);
- }
- }
- }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。