操作系统课程设计多进程同步橘子苹果问题源代码Word文档格式.docx
《操作系统课程设计多进程同步橘子苹果问题源代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统课程设计多进程同步橘子苹果问题源代码Word文档格式.docx(42页珍藏版)》请在冰点文库上搜索。
staticDecreaseapple[]appledecrease=newDecreaseapple[20];
staticDecreaseorange[]orangedecrease=newDecreaseorange[20];
//代表两个生产者对应线程的数目,i1为苹果,i2为橘子
staticinti1=0;
staticinti2=0;
//代表消费者对应线程的数目,d1为苹果,d2为橘子
staticintd1=0;
staticintd2=0;
staticTextAreatextArea1;
staticTextAreatextArea2;
staticJProgressBarprogressbar=newJProgressBar(0,20);
staticDrawdraw1;
staticJTextFieldt1;
staticJTextFieldt2;
staticJTextFieldt3;
staticJTextFieldt4;
publicvoidcreateMainWindow()
{
window=newJFrame("
橘子苹果问题"
);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(700,700);
window.setResizable(false);
JPanelpanel=newJPanel();
panel.setLayout(null);
JLabelApp1=newJLabel("
生产苹果者数:
"
App1.setBounds(10,500,100,25);
panel.add(App1);
progressbar.setStringPainted(true);
progressbar.setBounds(200,640,300,30);
panel.add(progressbar);
progressp=newprogress(myStorage);
p.start();
draw1=newDraw(myStorage);
draw1.setBounds(0,0,700,300);
panel.add(draw1);
t1=newJTextField();
t1.setBounds(120,500,60,25);
t1.addKeyListener(newKeyAdapter()
{
publicvoidkeyTyped(KeyEventevent)
{
charch=event.getKeyChar();
if(ch<
'
0'
||ch>
9'
)
{
event.consume();
}
}
});
panel.add(t1);
JButtoninapp1=newJButton("
增加"
inapp1.setBounds(190,500,60,25);
panel.add(inapp1);
inapp1.addActionListener(newActionListener()
@Override
publicvoidactionPerformed(ActionEvente)
if(i1<
20)
increaseappleProducer();
t1.setText(String.valueOf(i1));
JButtondeapp1=newJButton("
减少"
deapp1.setBounds(260,500,60,25);
panel.add(deapp1);
deapp1.addActionListener(newActionListener()
if(i1>
0)
decreaseappleProducer();
JLabelOrg1=newJLabel("
生产橘子者数:
Org1.setBounds(10,550,100,25);
panel.add(Org1);
t2=newJTextField();
t2.setBounds(120,550,60,25);
t2.addKeyListener(newKeyAdapter()
panel.add(t2);
JButtoninorg1=newJButton("
inorg1.setBounds(190,550,60,25);
panel.add(inorg1);
inorg1.addActionListener(newActionListener()
if(i2<
increaseorangeProducer();
t2.setText(String.valueOf(i2));
JButtondeorg1=newJButton("
deorg1.setBounds(260,550,60,25);
panel.add(deorg1);
deorg1.addActionListener(newActionListener()
if(i2>
decreaseorangeProducer();
//**消费者的数目设置苹果核橘子*/
JLabelApp2=newJLabel("
消费苹果者数:
App2.setBounds(330,500,100,25);
panel.add(App2);
t3=newJTextField();
t3.setBounds(440,500,60,25);
panel.add(t3);
JButtoninapp2=newJButton("
inapp2.setBounds(510,500,60,25);
panel.add(inapp2);
inapp2.addActionListener(newActionListener()
if(d1<
increaseappleConsumer();
t3.setText(String.valueOf(d1));
JButtondeapp2=newJButton("
deapp2.setBounds(580,500,60,25);
panel.add(deapp2);
deapp2.addActionListener(newActionListener()
if(d1>
decreaseappleConsumer();
JLabelOrg2=newJLabel("
消费橘子者数:
Org2.setBounds(330,550,100,25);
panel.add(Org2);
t4=newJTextField();
t4.setBounds(440,550,60,25);
panel.add(t4);
JButtoninorg2=newJButton("
inorg2.setBounds(510,550,60,25);
panel.add(inorg2);
inorg2.addActionListener(newActionListener()
if(d2<
increaseorangeConsumer();
t4.setText(String.valueOf(d2));
JButtondeorg2=newJButton("
deorg2.setBounds(580,550,60,25);
panel.add(deorg2);
deorg2.addActionListener(newActionListener()
if(d2>
decreaseorangeConsumer();
/**
*这里的代码是实现了设计要求中的第三条:
生产者速度可调。
*/
JLabelappsp1=newJLabel("
苹果生产速度:
appsp1.setBounds(10,400,100,25);
panel.add(appsp1);
finalJTextFieldtsp1=newJTextField();
tsp1.setBounds(120,400,60,25);
tsp1.setText(String.valueOf(Increaseapple.speed));
tsp1.addKeyListener(newKeyAdapter()
panel.add(tsp1);
JButtonappalter1=newJButton("
修改"
appalter1.setBounds(190,400,60,25);
panel.add(appalter1);
//在这里实现了控制生产速度的功能
appalter1.addActionListener(newActionListener()
Stringstr1=tsp1.getText();
longi=Long.parseLong(str1);
if(i<
=10000&
&
i>
=0)
Increaseapple.speed=i;
elseif(i<
Increaseapple.speed=0;
tsp1.setText(String.valueOf(Increaseapple.speed));
else
Increaseapple.speed=10000;
JLabelorgsp1=newJLabel("
橘子生产速度:
orgsp1.setBounds(10,450,100,25);
panel.add(orgsp1);
finalJTextFieldtsp2=newJTextField();
tsp2.setBounds(120,450,60,25);
tsp2.setText(String.valueOf(Increaseapple.speed));
tsp2.addKeyListener(newKeyAdapter()
panel.add(tsp2);
JButtonorgalter1=newJButton("
orgalter1.setBounds(190,450,60,25);
panel.add(orgalter1);
orgalter1.addActionListener(newActionListener()
Stringstr1=tsp2.getText();
tsp2.setText(String.valueOf(Increaseorange.speed));
消费者速度可调。
JLabelappsp2=newJLabel("
苹果消费速度"
appsp2.setBounds(260,400,100,25);
panel.add(appsp2);
finalJTextFieldtsp3=newJTextField();
tsp3.setBounds(370,400,60,25);
tsp3.setText(String.valueOf(Decreaseapple.speed));
tsp3.addKeyListener(newKeyAdapter()
panel.add(tsp3);
JButtonappalter2=newJButton("
appalter2.setBounds(440,400,60,25);
panel.add(appalter2);
//这里实现了控制消费速度的功能
appalter2.addActionListener(newActionListener()
Stringstr2=tsp3.getText();
longi=Long.parseLong(str2);
Decreaseapple.speed=i;
Decreaseapple.speed=0;
tsp3.setText(String.valueOf(Decreaseapple.speed));
Decreaseapple.speed=10000;
JLabelorgsp2=newJLabel("
橘子消费速度"
orgsp2.setBounds(260,450,100,25);
panel.add(orgsp2);
finalJTextFieldtsp4=newJTextField();
tsp4.setBounds(370,450,60,25);
tsp4.setText(String.valueOf(Decreaseapple.speed));
tsp4.addKeyListener(newKeyAdapter()
panel.add(tsp4);
JButtonorgalter2=newJButton("
orgalter2.setBounds(440,450,60,25);
panel.add(orgalter2);
orgalter2.addActionListener(newActionListener()
Stringstr2=tsp4.getText();