生产者和消费者问题

生产者和消费者问题

ID:38701034

大小:40.00 KB

页数:5页

时间:2019-06-17

生产者和消费者问题_第1页
生产者和消费者问题_第2页
生产者和消费者问题_第3页
生产者和消费者问题_第4页
生产者和消费者问题_第5页
资源描述:

《生产者和消费者问题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

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(max

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;i

7、Cost){try{n=Integer.parseInt(tCost.getText());}catch(Exceptione){n=3;}tCost.setText("3");}for(inti=0;i

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。