完整版者消费者问题 操作系统毕业课程设计java源代码Word文档格式.docx
《完整版者消费者问题 操作系统毕业课程设计java源代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《完整版者消费者问题 操作系统毕业课程设计java源代码Word文档格式.docx(39页珍藏版)》请在冰豆网上搜索。
importClass.Producer;
importClass.Semaphore;
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjavax.swing.event.*;
importjava.util.*;
publicclassMainextendsJFrame{
publicMainframe;
定义引用自身的主界面对象,在其他类中引用,以建立类间的联系
intp,c,b;
生产者数,消费数,缓冲区大小
JPanelp0;
JPanelp1;
JPanelp2;
JPanelp21;
JPanelp211,p212,p213;
JPanelp22;
JPanelp3;
JPanelp31;
JPanelp4;
JPanelp41;
JScrollPanep41;
JPanelp42;
JPanelp43;
JPanelp44;
JLabellb1;
JLabellb2;
JLabellb3;
JLabellb4;
JLabellb5;
JLabellb6;
JLabellb7;
publicJTextAreaa1;
JButtonb1;
JButtonb2;
JButtonb3;
JButtonb4;
JButtonb5;
publicJProgressBarpb1;
生产者生产速度进度条
publicJProgressBarpb2;
消费者消费速度进度条
publicJProgressBarpb3;
缓冲区剩余产品数进度条
publicJSliderjs1;
设置生产速度滑动竿
publicJSliderjs2;
设置消费熟读滑动杆
publicJSliderjs3;
信号量****************************************************************************************
Semaphorenotfull,notempty;
不满和不空信号量
Countnotfull_count,notempty_count;
InterfaceModuleIM;
Bufferbuffer;
=newBuffer(b,0);
*******************************************
*采用管程之后,此处的初始化应去掉
Semaphoremutex;
=newSemaphore
(1);
互斥信号量
Semaphorefull;
=newSemaphore(0);
同步信号量
Semaphoreempty;
=newSemaphore(b);
*
publicThread[]thread;
=newThread[100];
线程组
Thread[]thread1=newThread[100];
publicMain(Stringname,intpp,intcc,intbb){
TODOAuto-generatedconstructorstub
super(name);
this.p=pp;
由界面传入的生产者消费者以及缓冲区的数量
this.c=cc;
this.b=bb;
js1=newJSlider(0,10);
js2=newJSlider(0,10);
pb1=newJProgressBar(JProgressBar.VERTICAL,0,10);
进度条
pb2=newJProgressBar(JProgressBar.VERTICAL,0,10);
pb3=newJProgressBar(JProgressBar.VERTICAL,0,b);
pb1.setOrientation(JProgressBar.VERTICAL);
pb1.setStringPainted(true);
pb2.setStringPainted(true);
pb3.setStringPainted(true);
buffer=newBuffer(this.b,0);
*采用管程之后,此处应去掉
mutex=newSemaphore
(1);
互斥信号量
full=newSemaphore(0);
empty=newSemaphore(this.b);
*
frame=this;
buffer=newBuffer(b,0);
notfull=newSemaphore(0,frame);
IM=newInterfaceModule(frame);
notempty=newSemaphore(0,frame);
notfull_count=newCount(0);
notempty_count=newCount(0);
thread=newThread[100];
thread=newThread[100];
aa=this;
********************************************************
a1=newJTextArea(12,50);
p0=newJPanel();
主面板
p1=newJPanel();
p2=newJPanel();
p21=newJPanel();
p211=newJPanel();
p212=newJPanel();
p213=newJPanel();
p22=newJPanel();
p3=newJPanel();
p31=newJPanel();
p4=newJPanel();
p41=newJScrollPane(a1);
p42=newJPanel();
p43=newJPanel();
p44=newJPanel();
lb1=newJLabel("
生产者消费者"
JLabel.CENTER);
lb2=newJLabel("
生产产品的速度"
);
JLabel.VERTICAL);
lb3=newJLabel("
消耗产品的速度"
lb4=newJLabel("
缓冲区产品数"
lb5=newJLabel("
设置生产速度"
lb6=newJLabel("
设置消费速度"
lb7=newJLabel("
生产消费情况"
b1=newJButton("
开始"
b1.addActionListener(newStart());
b2=newJButton("
暂停"
b2.addActionListener(newStop());
b3=newJButton("
继续"
b3.addActionListener(newContinue());
b4=newJButton("
分析"
b4.addActionListener(newANalysis());
b5=newJButton("
退出"
b5.addActionListener(newExit());
Containercontainer=this.getContentPane();
container.setLayout(newBorderLayout());
p0.setLayout(newBorderLayout());
p1.setLayout(newFlowLayout());
p2.setLayout(newGridLayout(1,2));
p21.setLayout(newGridLayout(1,3));
p4.setLayout(newGridLayout(2,1));
p42.setLayout(newGridLayout(2,1));
add(p0);
p0.add(p1,BorderLayout.NORTH);
p0.add(p2,BorderLayout.CENTER);
p0.add(p3,BorderLayout.EAST);
p0.add(p4,BorderLayout.SOUTH);
p1.add(lb1);
p2.add(p21);
p21.setBackground(Color.GREEN);
p21.add(p211);
p21.add(p212);
p21.add(p213);
p211.add(lb2);
p211.add(pb1);
p212.add(lb3);
p212.add(pb2);
p213.add(lb4);
p213.add(pb3);
p22.add(lb5);
p22.add(js1);
p22.add(lb6);
p22.add(js2);
p2.add(p22);
p22.setBackground(Color.GRAY);
p3.add(b5);
p4.add(p41);
p4.add(p42);
p42.add(p43);
p42.add(p44);
p43.add(lb7,JLabel.CENTER);
p44.add(b1);
p44.add(b2);
p44.add(b3);
p44.add(b4);
p44.add(b5);
p1.setBackground(Color.GRAY);
p2.setBackground(Color.GRAY);
p3.setBackground(Color.GRAY);
p4.setBackground(Color.GRAY);
p3.setSize(100,100);
p31.setBackground(Color.GRAY);
pack();
setBackground(Color.lightGray);
setVisible(true);
DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();
设置主界居中显示
intscreenWidth=screenSize.width;
intscreenHeight=screenSize.(x,y);
}
classStartimplementsActionListener{
publicvoidactionPerformed(ActionEvente)
{
*采用管程之后,此处应去掉
for(inti=0;
i<
p;
i++)创建生产者线程
thread[i]=newThread(newProducer(Frame,"
"
+(i+1),mutex,full,empty,buffer));
for(inti=p;
p+c;
i++)创建消费者线程
thread[i]=newThread(newConsumer(Frame,"
+(i+1-p),mutex,full,empty,buffer));
i++)
thread[i].start();
启动所有线程
*
thread[i]=newThread(newProducer(frame,"
生产者"
+(i+1),IM,notfull,notempty,buffer,notfull_count,notempty_count));
for(inti=p;
thread[i]=newThread(newConsumer(frame,"
消费者"
+(i+1-p),IM,notfull,notempty,buffer,notfull_count,notempty_count));
for(inti=0;
thread[i].start();
}
}
classStopimplementsActionListener{
thread[i].suspend();
阻塞所有线程
classContinueimplementsActionListener{
thread[i].resume();
classANalysisimplementsActionListener{
newAnalysis("
buffer,frame);
classExitimplementsActionListener{
System.exit(0);
classwinCloseextendsWindowAdapter
{
publicvoidwindowClosing(WindowEvente)
}
**********************************************************************
*******************************Analysis.java结果分析类
importjava.lang.*;
importjava.io.*;
importJM.JM1;
publicclassAnalysisextendsJFrame{
JScrollPanep4;
JPanelp5;
Mainmain;
JTextAreayswh;
JScrollPaneklts;
publicAnalysis(Stringaa,Bufferbuffer,Mainmain){
super(aa);
setLayout(newBorderLayout());
this.buffer=buffer;
this.main=main;
p1=newJPanel();
p2=newJPanel();
p3=newJPanel();
yswh=newJTextArea(150,50);
p4=newJScrollPane(yswh);
yswh.setText(main.a1.getText());
p5=newJPanel();
klts=newJScrollPane(yswh);
p4=newJPanel();
thread[2].getState()
lb1=newJLabel("
模拟分析结果"
lb2=newJLabel("
生产者共生产了"
+buffer.getPnumber()+"
产品"
+"
lb3=newJLabel("
消费者共消费了"
+buffer.getCnumber()+"
lb4=newJLabel("
缓冲区剩余的产品数为"
+buffer.getValue(),JLabel.CENTER);
lb5=newJLabel("
生产者消费者各线程此刻所处的状态"
b1=newJButton("
确定"
b2=newJButton("
返回"
b1.addActionListener(newqueding());
b2.addActionListener(newquxiao());
add(p1,BorderLayout.NORTH);
p1.setLayout(newGridLayout(2,1));
p1.add(lb1);
p1.add(p2);
p2.add(lb1);
p1.add(p3);
p3.ad