1、完整版者消费者问题 操作系统毕业课程设计java源代码* Main主函数*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 jav
2、a.awt.event.ActionListener;import java.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 j
3、avax.swing.JProgressBar;import javax.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.Interfac
4、eModule;import Class.Producer;import Class.Semaphore;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.event.*;import java.util.*;public class Main extends JFrame public Main frame;定义引用自身的主界面对象,在其他类中引用,以建立类间的联系 int p,c,b;生产者数,消费数,缓冲区大小 JPanel p0; JPanel p1; JPanel p2;
5、 JPanel p21; JPanel p211,p212,p213; JPanel 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;
6、JButton b5; public JProgressBar pb1;生产者生产速度进度条 public JProgressBar pb2;消费者消费速度进度条 public JProgressBar pb3;缓冲区剩余产品数进度条 public JSlider js1;设置生产速度滑动竿 public JSlider js2;设置消费熟读滑动杆 public JSlider js3; 信号量* * Semaphore notfull,notempty;不满和不空信号量 Count notfull_count,notempty_count; InterfaceModule IM; Buffe
7、r buffer;=new Buffer(b,0);* * 采用管程之后,此处的初始化应去掉 Semaphore mutex;=new Semaphore(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-gene
8、rated constructor stub super(name); this.p=pp;由界面传入的生产者消费者以及缓冲区的数量 this.c=cc; this.b=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.set
9、Orientation(JProgressBar.VERTICAL); pb1.setStringPainted(true); pb2.setStringPainted(true); pb3.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 Semaph
10、ore(0,frame); IM=new InterfaceModule(frame); notempty=new Semaphore(0,frame); notfull_count=new Count(0); 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 JPane
11、l(); p212=new JPanel(); p213=new JPanel(); p22=new JPanel(); p3=new JPanel(); p31=new JPanel(); p4=new JPanel(); p41=new JScrollPane(a1); p42=new JPanel(); p43=new JPanel(); p44=new JPanel(); lb1=new JLabel(生产者消费者,JLabel.CENTER); lb2=new JLabel(生产产品的速度);,JLabel.VERTICAL); lb3=new JLabel(消耗产品的速度);,JL
12、abel.VERTICAL); lb4=new JLabel(缓冲区产品数);,JLabel.VERTICAL); lb5=new JLabel(设置生产速度);,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 JB
13、utton(继续); 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.setLayout(new FlowLayou
14、t(); 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(p21)
15、; 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
16、.add(lb6); p22.add(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.GRA
17、Y); p3.setSize(100, 100); p31.setBackground(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 ActionListener publ
18、ic void actionPerformed(ActionEvent e) * 采用管程之后,此处应去掉 for(int i=0;ip;i+)创建生产者线程 threadi=new Thread(new Producer(Frame,+(i+1),mutex,full,empty,buffer); for(int i=p;ip+c;i+)创建消费者线程 threadi=new Thread(new Consumer(Frame,+(i+1-p),mutex,full,empty,buffer); for(int i=0;ip+c;i+) threadi.start(); 启动所有线程 * f
19、or(int i=0;ip;i+)创建生产者线程 threadi=new Thread(new Producer(frame,生产者+(i+1),IM,notfull,notempty,buffer,notfull_count,notempty_count); for(int i=p;ip+c;i+)创建消费者线程 threadi=new Thread(new Consumer(frame,消费者+(i+1-p),IM,notfull,notempty,buffer,notfull_count,notempty_count); for(int i=0;ip+c;i+) threadi.star
20、t(); 启动所有线程 class Stop implements ActionListener public void actionPerformed(ActionEvent e) for(int i=0;ip+c;i+) threadi.suspend(); 阻塞所有线程 class Continue implements ActionListener public void actionPerformed(ActionEvent e) for(int i=0;ip+c;i+) threadi.resume(); class ANalysis implements ActionListen
21、er public void actionPerformed(ActionEvent e) new Analysis(分析,buffer,frame); class Exit implements ActionListener public void actionPerformed(ActionEvent e) System.exit(0); class winClose extends WindowAdapter public void windowClosing(WindowEvent e) System.exit(0); * Analysis.java 结果分析类package JM;i
22、mport 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; JLabel lb1; JLabel lb2; JLabel lb3; JLabel lb4; JLabel lb5; 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()
24、; p3=new JPanel(); yswh=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()+产品,JLabel.CENTER
25、); 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( 缓冲区剩余的产品数为,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.add(p2); p2.add(lb1); p1.add(p3); p3.ad
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1