1、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; JPanel
2、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; JButton
3、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; Buffer buffer;
4、=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-generated constructor
5、 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.setOrientation(JProgressBa
6、r.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 Semaphore(0,frame); IM=new In
7、terfaceModule(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 JPanel(); p212=new JPanel();
8、 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(消耗产品的速度 lb4=new JLabel(缓冲区产品数 lb5=n
9、ew JLabel(设置生产速度 lb6=new JLabel(设置消费速度 lb7=new JLabel(生产消费情况 b1=new JButton(开始 b1.addActionListener(new Start(); b2=new JButton(暂停 b2.addActionListener(new Stop(); b3=new JButton(继续 b3.addActionListener(new Continue(); b4=new JButton(分析 b4.addActionListener(new ANalysis(); b5=new JButton(退出 b5.addAc
10、tionListener(new Exit(); Container container=this.getContentPane(); container.setLayout(new BorderLayout(); p0.setLayout(new BorderLayout(); p1.setLayout(new FlowLayout(); p2.setLayout(new GridLayout(1,2); p21.setLayout(new GridLayout(1,3); p4.setLayout(new GridLayout(2,1); p42.setLayout(new GridLay
11、out(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(
12、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.se
13、tBackground(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); Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();设置主界居中显示
14、int screenWidth=screenSize.width; int screenHeight=screenSize.(x,y); class Start implements ActionListener public 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;p+c;i+)创建消费者线程 threadi=new
15、 Thread(new Consumer(Frame,+(i+1-p),mutex,full,empty,buffer);i+) threadi.start(); 启动所有线程 * threadi=new Thread(new Producer(frame,生产者+(i+1),IM,notfull,notempty,buffer,notfull_count,notempty_count); for(int i=p; threadi=new Thread(new Consumer(frame,消费者+(i+1-p),IM,notfull,notempty,buffer,notfull_count
16、,notempty_count); for(int i=0; threadi.start(); class Stop implements ActionListener threadi.suspend(); 阻塞所有线程 class Continue implements ActionListener threadi.resume(); class ANalysis implements ActionListener new Analysis(,buffer,frame); class Exit implements ActionListener System.exit(0); class w
17、inClose extends WindowAdapter public void windowClosing(WindowEvent e)* Analysis.java 结果分析类import java.lang.*;import java.io.*;import JM.JM1;public class Analysis extends JFrame JScrollPane p4; JPanel p5; Main main; JTextArea yswh; JScrollPane klts; public Analysis(String aa,Buffer buffer,Main main)
18、 super(aa); setLayout(new BorderLayout(); this.buffer=buffer; this.main=main; p1=new JPanel(); p2=new JPanel(); 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(); thread2.getState() lb1=
19、new JLabel(模拟分析结果 lb2=new JLabel(生产者共生产了+buffer.getPnumber()+产品+ lb3=new JLabel( 消费者共消费了+buffer.getCnumber()+ lb4=new JLabel( 缓冲区剩余的产品数为+buffer.getValue(),JLabel.CENTER); lb5=new JLabel(生产者消费者各线程此刻所处的状态 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