欢迎来到天天文库
浏览记录
ID:38701034
大小:40.00 KB
页数:5页
时间:2019-06-17
《生产者和消费者问题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjavax.swing.*;publicclassPCextendsJFrame{privatestaticintMAX=5;privatefinalJProgressBarjpb=newJProgressBar(SwingConstants.VERTICAL);privatefinalJTextAreajta=newJTextArea();privatefinalJTextFieldtProduce=newJTextField(3);privatefinal
2、JTextFieldtCost=newJTextField(3);privatefinalJTextFieldjtf_produce=newJTextField(3);privatefinalJTextFieldjtf_cost=newJTextField(3);privatefinalJTextFieldjtf_max=newJTextField(3);privatefinalJButtonbProduce=newJButton("生产");privatefinalJButtonbCost=newJButton("消费");privatefinalJButtonbAction=new
3、JButton("自动执行");privateBooleanflag=true;privateIntegermax=0;privatebooleandoProduce(intid){synchronized(max){try{MAX=Integer.parseInt(jtf_max.getText());}catch(Exceptione){MAX=5;jtf_max.setText("5");}if(max4、Value(max*100/MAX);returntrue;}returnfalse;}}privatebooleandoCost(intid){synchronized(max){try{MAX=Integer.parseInt(jtf_max.getText());}catch(Exceptione){MAX=5;jtf_max.setText("5");}if(max>0){max--;jta.setText("[C"+id+"]消耗了一个产品,现在的缓冲区:"+max+"/"+MAX+""+jta.getText());jpb.setValue(max*100/MAX);r5、eturntrue;}returnfalse;}}{jta.setText("");bProduce.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){doProduce(0);}});bCost.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){doCost(0);}});bAction.addActionListener(newActionListener(){publ6、icvoidactionPerformed(ActionEventae){if("自动执行".equals(bAction.getText())){bAction.setText("停止执行");flag=true;intn;synchronized(tProduce){try{n=Integer.parseInt(tProduce.getText());}catch(Exceptione){n=3;}tProduce.setText("3");for(inti=0;i7、Cost){try{n=Integer.parseInt(tCost.getText());}catch(Exceptione){n=3;}tCost.setText("3");}for(inti=0;i
4、Value(max*100/MAX);returntrue;}returnfalse;}}privatebooleandoCost(intid){synchronized(max){try{MAX=Integer.parseInt(jtf_max.getText());}catch(Exceptione){MAX=5;jtf_max.setText("5");}if(max>0){max--;jta.setText("[C"+id+"]消耗了一个产品,现在的缓冲区:"+max+"/"+MAX+""+jta.getText());jpb.setValue(max*100/MAX);r
5、eturntrue;}returnfalse;}}{jta.setText("");bProduce.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){doProduce(0);}});bCost.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){doCost(0);}});bAction.addActionListener(newActionListener(){publ
6、icvoidactionPerformed(ActionEventae){if("自动执行".equals(bAction.getText())){bAction.setText("停止执行");flag=true;intn;synchronized(tProduce){try{n=Integer.parseInt(tProduce.getText());}catch(Exceptione){n=3;}tProduce.setText("3");for(inti=0;i7、Cost){try{n=Integer.parseInt(tCost.getText());}catch(Exceptione){n=3;}tCost.setText("3");}for(inti=0;i
7、Cost){try{n=Integer.parseInt(tCost.getText());}catch(Exceptione){n=3;}tCost.setText("3");}for(inti=0;i
此文档下载收益归作者所有