亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

生產者消費者模式死鎖

生產者消費者模式死鎖

fenkapian 2018-10-21 21:41:44
public?class?ProducerConsumer<T>?{ ????private?final?LinkedList<T>?linkedList?=?new?LinkedList<>(); ????private?final?long?MAX?=?10; ????private?int?count?=?0; ????public?synchronized?void?put(T?t)?{ ????????while?(linkedList.size()?==?MAX)?{ ????????????try?{ ????????????????this.wait(); ????????????}?catch?(InterruptedException?e)?{ ????????????????e.printStackTrace(); ????????????} ????????} ????????linkedList.add(t); ????????count++; ????????this.notifyAll(); ????} ????public?synchronized?T?get()?{ ????????while?(linkedList.size()?==?0)?{ ????????????try?{ ????????????????this.wait(); ????????????}?catch?(InterruptedException?e)?{ ????????????????e.printStackTrace(); ????????????} ????????} ????????T?t?=?null; ????????t?=?linkedList.removeFirst(); ????????count--; ????????this.notifyAll(); ????????return?t; ????} ????public?static?void?main(String[]?args)?{ ????????ProducerConsumer<String>?pc?=?new?ProducerConsumer<>(); ????????for?(int?i?=?0;?i?<?2;?i++)?{ ????????????new?Thread(()?->?{ ????????????????for?(int?j?=?0;?j?<?6;?j++)?{ ????????????????????pc.put(Thread.currentThread().getName()?+?"?"); ????????????????????try?{ ????????????????????????Thread.sleep(2); ????????????????????}?catch?(InterruptedException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????} ????????????},?"producer?"?+?i).start(); ????????} ????????for?(int?i?=?0;?i?<?10;?i++)?{ ????????????new?Thread(()?->?{ ????????????????for?(int?j?=?0;?j?<?5;?j++)?{ ????????????????????System.out.println(pc.get()); ????????????????????try?{ ????????????????????????Thread.sleep(2); ????????????????????}?catch?(InterruptedException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????} ????????????},?"consumer?"?+?i).start(); ????????} ????} }代碼運行就死鎖? 哪里錯了啊
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 1835 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號