生产者与消费者.docx
- 文档编号:11121283
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:20
- 大小:1.95MB
生产者与消费者.docx
《生产者与消费者.docx》由会员分享,可在线阅读,更多相关《生产者与消费者.docx(20页珍藏版)》请在冰豆网上搜索。
生产者与消费者
广州恒邦网络技术有限公司正诚聘英才,您应聘的职位是Java工程师,请你于今天(星期五)下午2点左右到公司来参加面试。
公司的上班时间是5天制,双休。
上班时间为8:
30~12:
00,13:
30~18:
00
公交路线:
华景新城BRT站、华景路口(B9路车)都可以直达公司。
附近地铁站位于岗顶站。
公司地址:
广州市天河区中山大道西89号天河软件园华景区C栋中梯804室。
classTicketsSystem{publicstaticvoidmain(String[]args){SellThreadst=newSellThread();newThread(st).start();try{Thread.sleep
(1);}catch(Exceptione){e.printStackTrace();}st.b=true;newThread(st).start();//newThread(st).start();//newThread(st).start();}}classSellThreadimplementsRunnable{inttickets=100;Objectobj=newObject();booleanb=false;publicvoidrun(){if(b==false){while(true)sell();}else{while(true){synchronized(obj){try{Thread.sleep(10);}catch(Exceptione){e.printStackTrace();}synchronized(this){if(tickets>0){System.out.println("obj:
"+Thread.currentThread().getName()+"selltickets:
"+tickets);tickets--;}}}}}}publicsynchronizedvoidsell(){synchronized(obj){if(tickets>0){try{Thread.sleep(10);}catch(Exceptione){e.printStackTrace();}System.out.println("sell():
"+Thread.currentThread().getName()+"selltickets:
"+tickets);tickets--;}}}}
publicclassTest2{
publicstaticvoidmain(String[]args){
Queueq=newQueue();
Producerp=newProducer(q);
Consumerc=newConsumer(q);
p.start();
c.start();
}
}
classProducerextendsThread{
Queueq;
Producer(Queueq){
this.q=q;
}
publicvoidrun(){
for(inti=0;i<100;i++){
q.put(i);
System.out.println("Producerput"+i);
}
}
}
classConsumerextendsThread{
Queueq;
Consumer(Queueq){
this.q=q;
}
publicvoidrun(){
while(true){
System.out.println("Consumerget"+q.get());
}
}
}
classQueue{
intvalue;
booleanbFull=false;
publicsynchronizedvoidput(inti){
if(!
bFull){
value=i;
bFull=true;
notify();
}
try{
wait();
}catch(Exceptione){
e.printStackTrace();
}
}
publicsynchronizedintget(){
if(!
bFull){
try{
wait();
}catch(Exceptione){
e.printStackTrace();
}
}
bFull=false;
notify();
returnvalue;
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 生产者 消费者
![提示](https://static.bdocx.com/images/bang_tan.gif)