(完整版)者消费者问题操作系统毕业课程设计java源代码_第1页
(完整版)者消费者问题操作系统毕业课程设计java源代码_第2页
(完整版)者消费者问题操作系统毕业课程设计java源代码_第3页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、*package JM;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import ja

2、va.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.util.Date;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax

3、.swing.JScrollPane;import javax.swing.JSlider;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import Class.Buffer;import Class.Consumer;import Class.Count;import Class.InterfaceModule;import Class.Producer;import

4、 Class.Semaphore;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.event.*;import java.util.*;public class Main extends JFramepublic Main frame; 定义引用自身的主界面对象,在其他类中引用,以建立类间的联系int p,c,b; 生产者数,消费数,缓冲区大小JPanel p0;JPanel p1;JPanel p2;JPanel p21;JPanel p211,p212,p213;JPanel

5、 p22;JPanel p3;JPanel p31;JPanel p4;JPanel p41;JScrollPane p41;JPanel p42;JPanel p43;JPanel p44;JLabel lb1;JLabel lb2;JLabel lb3;JLabel lb4;JLabel lb5;JLabel lb6;JLabel lb7;public JTextArea a1;JButton b1;JButton b2;JButton b3;JButton b4;JButton b5;public JProgressBar pb1;public JProgressBar pb2;publ

6、ic JProgressBar pb3;生产者生产速度进度条消费者消费速度进度条缓冲区剩余产品数进度条public JSlider js1; 设置生产速度滑动竿public JSlider js2; 设置消费熟读滑动杆public JSlider js3;信号*不满和不空信号量*Semaphore notfull,notempty;Count notfull_count,notempty_count;InterfaceModule IM;Buffer buffer;=new Buffer(b,0);* 采用管程之后,此处的初始化应去掉Semaphore mutex;=new Semaphore

7、(1);互斥信号量Semaphore full;=new Semaphore(0); 同步信号量Semaphore empty;=new Semaphore(b);同步信号量public Thread thread; =new Thread100;线程组Thread thread1 =new Thread100;public Main(String name,int pp,int cc,int bb) TODO Auto-generated constructor stub super(name);this.p=pp; 由界面传入的生产者消费者以及缓冲区的数量 this.c=cc;this.b

8、=bb;js1=new JSlider(0,10);js2=new JSlider(0,10);进度条进度条进度条pb1=new JProgressBar(JProgressBar.VERTICAL,0,10);pb2=new JProgressBar(JProgressBar.VERTICAL,0,10);pb3=new JProgressBar(JProgressBar.VERTICAL,0,b);pb1.setOrientation(JProgressBar.VERTICAL);pb1.setStringPainted(true);pb2.setStringPainted(true);p

9、b3.setStringPainted(true);buffer=new Buffer(this.b,0);互斥信号量* 采用管程之后,此处应去掉mutex=new Semaphore(1);full=new Semaphore(0); empty=new Semaphore(this.b);*frame=this;buffer=new Buffer(b,0);notfull=new Semaphore(0,frame);IM=new InterfaceModule(frame);notempty=new Semaphore(0,frame);notfull_count=new Count(0

10、);notempty_count=new Count(0);thread=new Thread100;thread=new Thread100;aa=this;a1=new JTextArea(12,50);p0=new JPanel(); 主面板p1=new JPanel();p2=new JPanel();p21=new JPanel();p211=new JPanel();p212=new JPanel();p213=new JPanel();p22=new JPanel();p3=new JPanel();p31=new JPanel();p4=new JPanel();p41=new

11、 JScrollPane(a1);p42=new JPanel();p43=new JPanel();p44=new JPanel();lb1=new JLabel(" 生产者消费者 ",JLabel.CENTER);lb2=new JLabel("生产产品的速度 ");,JLabel.VERTICAL);lb3=new JLabel("消耗产品的速度 ");,JLabel.VERTICAL);lb4=new JLabel("缓冲区产品数 ");,JLabel.VERTICAL);lb5=new JLabel(&q

12、uot;设置生产速度 ");,JLabel.VERTICAL);lb6=new JLabel("设置消费速度 ");,JLabel.VERTICAL);lb7=new JLabel("生产消费情况 ");,JLabel.VERTICAL);b1=new JButton("开始 ");b1.addActionListener(new Start();b2=new JButton("暂停 ");b2.addActionListener(new Stop();b3=new JButton("继续 &q

13、uot;);b3.addActionListener(new Continue();b4=new JButton("分析 ");b4.addActionListener(new ANalysis();b5=new JButton(" 退出 ");b5.addActionListener(new Exit();Container container=this.getContentPane(); container.setLayout(new BorderLayout();p0.setLayout(new BorderLayout();p1.setLayou

14、t(new FlowLayout();p2.setLayout(new GridLayout(1,2);p21.setLayout(new GridLayout(1,3);p4.setLayout(new GridLayout(2,1);p42.setLayout(new GridLayout(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(

15、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);p22.add(lb5);p22.add(js1);p22.add(lb6);p22.a

16、dd(js2);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.set

17、Background(Color.GRAY);pack();setBackground(Color.lightGray);设置setVisible(true);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();主界居中显示int screenWidth=screenSize.width;int screenHeight=screenSize.(x,y);class Start implements ActionListenerpublic void actionPerformed(ActionEvent e)* 采

18、用管程之后,此处应去掉for(int i=0;i<p;i+) 创建生产者线程Producer(Frame,""+(i+1),mutex,full,empty,buffer);for(int i=p;i<p+c;i+)创建消费者线程threadi=new Thread(newConsumer(Frame,""+(i+1-p),mutex,full,empty,buffer);for(int i=0;i<p+c;i+)threadi.start(); 启动所有线程*for(int i=0;i<p;i+) 创建生产者线程threadi=

19、new Thread(new Producer(frame," 生 产 者"+(i+1),IM,notfull,notempty,buffer,notfull_count,notempty_count);for(int i=p;i<p+c;i+) 创建消费者线程threadi=new Thread(new Consumer(frame," 消费者 "+(i+1-p),IM,notfull,notempty,buffer,notfull_count,notempty_count);for(int i=0;i<p+c;i+)threadi.sta

20、rt(); 启动所有线程class Stop implements ActionListenerpublic void actionPerformed(ActionEvent e)for(int i=0;i<p+c;i+)threadi.suspend(); 阻塞所有线程 class Continue implements ActionListenerpublic void actionPerformed(ActionEvent e)for(int i=0;i<p+c;i+)threadi.resume();class ANalysis implements ActionListe

21、nerpublic void actionPerformed(ActionEvent e)new Analysis(" 分析 ",buffer,frame);class Exit implements ActionListenerpublic void actionPerformed(ActionEvent e)System.exit(0);class winClose extends WindowAdapterpublic void windowClosing(WindowEvent e)System.exit(0);*Analysis.java结果分析类*package

22、 JM;import java.lang.*;import java.io.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import Class.Buffer;import JM.JM1;import Class.Buffer;public class Analysis extends JFrame JPanel p1;JPanel p2;JPanel p3;JScrollPane p4;JPanel p5;Buff

23、er buffer;JLabellb1;JLabellb2;JLabellb3;JLabellb4;JLabellb5;JButton b1;JButton b2;Main main;JTextArea yswh;JScrollPane klts;public Analysis(String aa,Buffer buffer,Main main) super(aa);setLayout(new BorderLayout();this.buffer=buffer;this.main=main;p1=new JPanel();p2=new JPanel();p3=new JPanel();yswh

24、=new JTextArea(150,50);p4=new JScrollPane(yswh);yswh.setText(main.a1.getText();p5=new JPanel();klts=new JScrollPane(yswh);p4=new JPanel();p5=new JPanel();thread2.getState() lb1=new JLabel(" 模拟分析结果 ",JLabel.CENTER);lb2=new JLabel(" 生产者共生产了 "+buffer.getPnumber()+" 产 品",JL

25、abel.CENTER);lb2=new JLabel("生产者共生产了 "+" 产品 ",JLabel.CENTER);lb3=new JLabel("消费者共消费了 "+buffer.getCnumber()+品 ",JLabel.CENTER);lb3=new JLabel("消费者共消费了 ",JLabel.CENTER);lb4=new JLabel("缓冲区剩余的产品数"+buffer.getValue(),JLabel.CENTER);lb4=new JLabel(&qu

26、ot;缓冲区剩余的产品数为,JLabel.CENTER);lb5=new JLabel("生产者消费者各线程此刻所处的状态",JLabel.CENTER);b1=new JButton(" 确定 ");b2=new JButton(" 返回 "); b1.addActionListener(new queding(); b2.addActionListener(new quxiao();add(p1,BorderLayout.NORTH);p1.setLayout(new GridLayout(2, 1);p1.add(lb1);p1

27、.add(p2);p2.add(lb1);p1.add(p3);p3.add(lb2);p3.add(lb3);p3.add(lb4);add(p4,BorderLayout.CENTER );p4.add(lb5);p4.add(klts);add(p5,BorderLayout.SOUTH );p5.add(b1);p5.add(b2);setBackground(Color.lightGray);设置主界setVisible(true);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();居中显示int scr

28、eenWidth=screenSize.width;int screenHeight=screenSize.(x,y);for(int i=0;i<10;i+)yswh.setText(""+this.main.threadi.getState();for(int i=0;i<10;i+)System.out.print(""+main.threadi.getState()+"n");main.a1.append(""+main.threadi.getState()+"n");mai

29、n.a1.append(""+main.threadi.isAlive()+"n");class queding implements ActionListenerpublic void actionPerformed(ActionEvent e)dispose();class quxiao implements ActionListenerpublic void actionPerformed(ActionEvent e)dispose();*界面 JM1.javapackage JM;import java.lang.*;import java.io

30、.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class JM1 extends JFrame setLayout(new GridLayout(3,3);JPanel p1;JPanel p2;JPanel p21;JPanel p22;JPanel p3;JPanel p31;JPanel p32;JPanel p4;JPanel p41;JPanel p42;JPanel p5;JLabellb1

31、;JLabellb2;JLabellb3;JLabellb4;JLabellb5;JTextField f1;JTextField f2;JTextField f3;JTextField f4;JButton b1;JButton b2;public JM1()public JM1(String aa)super(aa);setLayout(new GridLayout(4,1);p1=new JPanel();p2=new JPanel();p21=new JPanel();p22=new JPanel();p3=new JPanel();p31=new JPanel();p32=new J

32、Panel();p4=new JPanel();p41=new JPanel();p42=new JPanel();p5=new JPanel();lb1=new JLabel("生产者消费者仿真 ",JLabel.CENTER);lb2=new JLabel("请输入生产者数目 ",JLabel.CENTER);lb3=new JLabel("请输入消费者数目 ",JLabel.CENTER);lb4=new JLabel("请输入缓冲区数目 ",JLabel.CENTER);lb5=new JLabel(&qu

33、ot;请输入生产者,消费者以及缓冲区的数目,以“确定”结束 ",JLabel.CENTER);f1=new JTextField(8);f2=new JTextField(8);f3=new JTextField(8);f1.setText("6");f2.setText("5");f3.setText("50");f4=new JTextField();b1=new JButton(" 确定 ");b2=new JButton(" 取消 ");b1.addActionListener

34、(new queding();b2.addActionListener(new quxiao();p1.setLayout(new GridLayout(2,1);p1.add(lb1);p1.add(lb5);p1.add(f1);p2.setLayout(new GridLayout(3,1);p2.add(p21);p2.add(p31);p2.add(p32);p21.add(lb2);p21.add(f1);p31.add(lb3);p31.add(f2);p32.add(lb4);p32.add(f3);p4.setLayout(new GridLayout(1,2);p4.add

35、(p41);p4.add(p42);p41.add(b1);p41.add(b2);p5.add(lb5);add(p1);add(p2);add(p3);add(p4);add(p5);setBackground(Color.lightGray);setVisible(true);设置参数Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); 将界面居中显示int screenWidth=screenSize.width;int screenHeight=screenSize.(x,y);class queding

36、implements ActionListener public void actionPerformed(ActionEvent e)dispose();int producer=Integer.parseInt(f1.getText();int consumer=Integer.parseInt(f2.getText();int buffer=Integer.parseInt(f3.getText();new Main(" 欢 迎 您 使 用 生 产 者 消 费 者 仿 真 系 统",producer,consumer,buffer);class quxiao impl

37、ements ActionListenerpublic void actionPerformed(ActionEvent e)f1.setText("");f2.setText("");f3.setText("");public static void main(String args)JM1 dhk=new JM1(" 生产者消费者 ");Buffer.java 缓冲区类缓冲区的大小缓冲区中的产品数*package Class;import java.awt.*;import javax.swing.*;impo

38、rt java.awt.event.*;import javax.swing.event.*;import java.util.*;Buffer 类 Buffer :public class Bufferpublic int Bsize;public int Bvalue;public int Pnumber; 生产者生产的产品计数public int Cnumber; 消费者消费的产品计数public Buffer(int size,int value)this.Bsize=size;this.Bvalue=value;public int getValue() return Bvalue;

39、public int getPnumber() return Pnumber;public int getCnumber() return Cnumber;获取 Value 的值获取生产者生产的产品总数获取消费者消费的总产品数值*package Class;import java.awt.*;import javax.swing.*;import java.awt.event.*; import javax.swing.event.*;import JM.Main;Consumer.java 消费者类import java.util.*;public class Consumer implem

40、ents Runnable 消费者类private String name; 消费者的名字private InterfaceModule IM;管程的引用Count aabc,ccba; 生产者消费者阻塞的信号量计数 private Semaphore notfull; privateSemaphore notempty;private Main frame; 主界面public Consumer(Main frame,String name,Semaphore mutex,Semaphore full,Semaphore empty,Buffer buffer)public Consumer

41、(Main frame,String name,InterfaceModule im,Semaphore notfull,Semaphore notempty,Buffer buffer,Count aabc,Count ccba)this.buffer=buffer;this.notempty=notempty; 消费者阻塞后移入队列的信号量 this.notfull=notfull; 生产者阻塞后移入队列的信号量this.IM=im;=name;this.aabc=aabc; 为消费者阻塞个数计数this.ccba=ccba; 为生产者阻塞的个数计数值this.frame

42、=frame;消费者阻塞后移入队public void run() while(true)this.IM.enter(""+);消费者线程调用管程System.out.print(+"已经进入了管程 n");frame.a1.append(+"已经进入了管程 n");if(buffer.Bvalue=0)缓冲区为空,即没有产品,消费者线程调用 Wait 等待this.IM.Wait(notempty,ccba,);notempty列的信号量 , 初值为 0 ccb

43、a 为消费者阻塞个数计数buffer.Bvalue-; 缓冲区中的产品数减 1 buffer.Cnumber+; 消费者消费的总次数计数System.out.print(+"frame.a1.append(+"消耗了一件产品 "+buffer.Bvalue+"n");从缓冲区取走并消耗一件产品此刻缓冲区的产品数为"+buffer.Bvalue+"缓冲区总容量为生产了一件产品并放入缓冲区此刻"+buffer.Bsize+"n");frame.a1.append(

44、+"缓冲区的产品数为"+buffer.Bvalue+"缓冲区总容量为 "+buffer.Bsize+"n");if(buffer.Bvalue>=0)frame.pb2.setValue(frame.js2.getValue();frame.pb3.setValue(buffer.Bvalue);frame.pb3.setString(String.valueOf(buffer.Bvalue);notfull 为生产this.IM.Signal(notfull,aabc,); 唤醒等待的生产者

45、者阻塞后移入队列的信号量 aabc 为生产者阻塞的个数计数值this.IM.leave();System.out.print(+"离开管程 n");frame.a1.append(+"离开管程 n");tryThread.sleep(500*(10-frame.js2.getValue();catch(InterruptedException e)while(true)*full.p();mutex.p();frame.a1.insert(""+" 消费者 "+

46、name+" 从缓冲区中取出了一件产品 n",0);frame.a1.insert("n",0);frame.a1.insert(". .n",0);buffer.Cnumber+;buffer.Bvalue-;Buffer的数据数减 1if(buffer.Bvalue>=0) frame.pb2.setValue(frame.js2.getValue();frame.pb3.setValue(buffer.Bvalue); frame.pb3.setString(String.valueOf(buffer.Bvalue);mu

47、tex.v();empty.v();tryif(frame.js2.getValue()=0)Thread.sleep(20000);elseThread.sleep(100*(10-frame.js2.getValue();catch(InterruptedException e)计数* C package Class;public class Count public int Cvalue;public Count(int aa) TODO Auto-generated constructor stubthis.Cvalue=aa;public void setValue(int aaa)

48、this.Cvalue=aaa;public int getValue()return this.Cvalue;* InterfaceModule.java管程类package Class;import JM.Main;public class InterfaceModule private Semaphore mutex; 进程调用管程之前所使用的互斥信号量private Semaphore next;发出 signal 操作的线程挂起自己的信号量int next_count; 在 next上等待的线程数Main frame;Count x_count; public Count count

49、;public InterfaceModule(Main frame) TODO Auto-generated constructor stubthis.count=count;this.frame=frame;mutex=new Semaphore(1,frame); 初始化互斥信号量next=new Semaphore(0,frame);next_count=0;this.x_count=x_count;public void enter(String s)mutex.p(s+" 因无法进入管程而 "); 互斥的进入管程frame.a1.append("&qu

50、ot;+s+" 已经进入管程 n");System.out.print("n 进入管程 n");public void leave(String s1)frame.a1.append(s1+" 当前正在执行管程的 leave 操作 n"); if(next_count>0) 判断有否发出 signal 操作的线程 next.v(s1+" 释放一个因发出 signal 操作而阻塞自己的线程 n"); 若有就 释放一个frame.a1.append(" 释放一个因发出 signal 操作而阻塞自己的线程

51、 n");elsemutex.v(s1+" 离开管程 n"+" 开放管程 "); 否则开放管程n");frame.a1.append(" 线程即将要离开管程,在离开之前开放管程System.out.print("n 离开管程 n");public void Wait(Semaphore x_sem,Count x_count,String s1)x_count.Cvalue+; 等待资源的线程数加1,初始值为 0frame.a1.append();System.out.print("Waitn&

52、quot;);frame.a1.append(s1+" 执行 Wait 操作 因资源不可用而该线程即将阻塞自己!(缓冲区已满或者已为空) n 在阻塞自己之前,先判断是否有发出signal操作的线程。若有,则释放之。否则准备开放管程。之后便阻塞自己 n ");if(next_count>0) 判断是否有发出 signal 操作的线程。 因为发出此操作的线程 会阻塞自己。next.v(" 释放一个因发出 signal 操作,唤醒了其他线程而阻塞自己的线 程 现在 n"); 若有就释放一个System.out.print(" 释放一个发出 si

53、gnal 操作的线程 n");frame.a1.append(" 释放一个因发出 signal 操作,唤醒了其他线程而阻塞自己的线程 n");elseframe.a1.append(" 没有因发出 signal 操作而阻塞自己的线程, 也没有 在阻塞当前线程之前先开放管程,让其他线程有机会获得当前线程的可用资源管程 n");mutex.v(" 没有因发出 signal 操作而阻塞自己的线程,也没有当前线程的可用资源 在阻塞当前线程之前先开放管程,让其他线程有机会获得管程 n"); 否则开放管程System.out.print

54、(" 开放管程 n");frame.a1.append(" 当前线程已经因资源不足而阻塞自己 n");x_sem.p(s1+" 线程因没有可用资源 (即缓冲区) 而 "); 等待资源的线程阻塞自己, X_sem 初始化为 0System.out.print("Waitn");x_count.Cvalue-; 等待资源的线程数减 1public void Signal(Semaphore x_sem,Count x_count,String s2)frame.a1.append(s2+" 执行 Signal 操作 若当前有等待资源的线程则唤 醒该线程并阻塞自己。否则唤醒信号丢失 n ");if(x_count.Cvalue>0) 判断是否有等待资源的线程System.out.print("Signaln");next_count+; 发出 signal 操作的线程数加 1frame.a1.append("Signal 操作 资源可用,唤醒等待资源的线程! !(缓冲区不满或者不空) n");x_sem.v("Signal 操作资源可用,唤醒等待资源的线程! (缓冲区不满或者不空

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论