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

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

java多線程,類似哲學(xué)家進(jìn)餐問題

java多線程,類似哲學(xué)家進(jìn)餐問題

ibeautiful 2019-04-19 13:15:49
兩個(gè)人吃飯,但只有一個(gè)勺子和叉子,只有同時(shí)獲得勺子和叉子才能吃到飯。兩個(gè)人是兩個(gè)線程,線程的run方法是獲取勺子和叉子并eat。eat方法加了synchronized 但為什么還是不能互斥,兩個(gè)人不是同一時(shí)刻只能有一個(gè)人吃么?public class Tool {     private final String name;     public Tool(String name){         this.name=name;     } }public class EaterThread extends Thread{     private final Tool leftHand;     private final Tool rightHand;     private String personName;     public EaterThread(String personName,Tool leftHand,Tool rightHand) {         this.leftHand=leftHand;         this.rightHand=rightHand;         this.personName=personName;     }     public synchronized void eat(){                 System.out.println(this.personName+"拿起"+this.leftHand);                 System.out.println(this.personName+"拿起"+this.rightHand);                 System.out.println(this.personName+"吃飯");                 System.out.println(this.personName+"放下"+this.leftHand);                 System.out.println(this.personName+"放下"+this.rightHand);             }     public void run(){         while(true){             this.eat();             try {                 Thread.sleep(1000);             } catch (Exception e) {}         }     } }public class Main {     public static void main(String[] args) {         Tool spoon=new Tool("spoon");         Tool fork=new Tool("fork");         new EaterThread("Tom", spoon, fork).start();         new EaterThread("Jerry", spoon, fork).start();     } }為什么會(huì)出現(xiàn)下面的情況:同一時(shí)刻不是只能有一個(gè)人進(jìn)入臨界區(qū)么,怎么Tom和Jerry為什么會(huì)同時(shí)進(jìn)入?大佬們幫幫忙,想不明白。。。。
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 579 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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