package new_last;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
//import ceshi2.ProducerAndConsumer;
public class Work_interface extends JFrame implements ActionListener {
private static int m = 50;// 速度控制量
private static int i = 1;// 暂停控制量
private static int counter = 0;// 缓冲池的产品数量
public static void i() {
Work_interface.i = 0;
}
public static int getM() {
return m;
}
public static void increase() {
Work_interface.m = Work_interface.m + 1;
}
public static void decrease() {
if (Work_interface.m >= 1)
Work_interface.m = Work_interface.m - 1;
}
JLabel lbl1 = new JLabel("生产消费演示");
JTextArea txt = new JTextArea(200, 300);
JScrollPane jsp = new JScrollPane(txt);// 增加滚动条
JButton btn1 = new JButton("运行");
JButton btn2 = new JButton("退出");
JButton btn3 = new JButton("生产加速");
JButton btn4 = new JButton("消费加速");
JButton btn5 = new JButton("暂停");
JLabel lbl2 = new JLabel(" 缓冲池内的产品数量");
JTextField txt_1 = new JTextField(5);
class Producer implements Runnable { //生产者线程类
ProducerAndConsumer queue;
public Producer(ProducerAndConsumer s) {
queue = s;
}
public void run() {
for (;;) {
char c = (char) (Math.random() * 26 + 'A');//随机的产生一个字母
queue.inqueue(c);
try {
Thread.sleep(Work_interface.getM());
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class Consumer implements Runnable { //消费者线程类
ProducerAndConsumer queue;
public Consumer(ProducerAndConsumer s) {
queue = s;
}
public void run() {
for (;;) {
queue.outqueue();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class ProducerAndConsumer { //资源调用类
private int head = 0;// 队头
private int tail = 1;// 队尾
public final int Num = 20;// 缓冲区大小
private int mutex = 1;// 互斥信号量
// public int kongzhi = 1;// 控制信号量
public char data[] = new char[Num];
public synchronized void inqueue(char c) {// 防止多个线程同时访问这个对象的synchronized方法,线程同步
if (i == 0) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
if (mutex == 0) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
mutex--;
while ((tail) % Num == head) {
txt.append("队列满,生产等待." + "\n");
txt_1.setText(counter + "");
// txt_1.setText(counter+"");
try {
mutex++;
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
this.notify();
data[tail] = c;
tail = (tail + 1) % Num;
counter++;
txt.append("生产:" + c + "\n");
txt_1.setText(counter + "");
mutex++;
}
public synchronized char outqueue() {
if (i == 0) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
if (mutex == 0) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
mutex--;
while ((head + 1) % Num == tail) {
txt.append("队列空,消费等待." + "\n");
txt_1.setText(counter + "");
// txt_1.setText(counter+"");
try {
mutex++;
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
this.notify();
head = (head + 1) % Num;
txt.append("消费:" + data[head] + "\n");
txt_1.setText(counter + "");
counter--;
mutex++;
// txt.append("控制值:" + Work_interface.getM());
return data[head];
}
}
public Work_interface(String str) {
this.setTitle(str);
final JPanel jp = (JPanel) this.getContentPane();
// jp.setLayout(new GridBagLayout());
GridBagLayout grid = new GridBagLayout();// 使用网格包布局
GridBagConstraints c = new GridBagConstraints();
setLayout(grid);
c.fill = GridBagConstraints.NONE;//不重新调整组件大小
// jp.add(jsp,grid,c);
// jp.add(lbl1);
// jp.add(btn1);
// jp.add(btn2);
// Label lbl1 = new Label("生产消费演示");
//
// TextArea txt = new TextArea(20, 30);
//
// JScrollPane jsp = new JScrollPane(txt);// 增加滚动条
//
// Button btn1 = new Button("演示");
//
// Button btn2 = new Button("退出");
c.weightx = 1.8;//额外控件分布
c.weighty = 1;
grid.setConstraints(btn1, c);
c.weightx = 1;
c.weighty = 1;
grid.setConstraints(btn5, c);
c.weightx = 1;
c.weighty = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
grid.setConstraints(btn2, c);
c.weightx = 1;
c.weighty = 1;
// c.gridwidth =1;
grid.setConstraints(btn3, c);
c.weightx = 1;
c.weighty = 2;
// c.gridwidth = GridBagConstraints.REMAINDER;
grid.setConstraints(btn4, c);
c.weightx = 1;
c.weighty = 1;
grid.setConstraints(lbl1, c);
c.weightx = 1;
c.weighty = 13;
// c.gridheight = 3;
// c.gridwidth = 3;
c.fill = GridBagConstraints.BOTH;
grid.setConstraints(jsp, c);
// c.fill = GridBagConstraints.NONE;
c.gridwidth = 2;//控件分布占的单元格数
c.weightx = 1;
c.weighty = 1;
grid.setConstraints(lbl2, c);
c.gridwidth = 3;
c.weightx = 2;
c.weighty = 1;
grid.setConstraints(txt_1, c);
jp.add(btn1);
jp.add(btn5);
jp.add(btn2);
jp.add(btn3);
jp.add(btn4);
jp.add(lbl1);
jp.add(jsp);
jp.add(lbl2);
jp.add(txt_1);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
addWindowListener(new WindowAdapter() {// 如果按 X, 关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(final ActionEvent ae) {
if (ae.getSource() == btn1) {
txt.setText(null);
Work_interface.i = 1;
ProducerAndConsumer queue = new ProducerAndConsumer();
Runnable p = new Producer(queue);
Runnable c = new Consumer(queue);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
Thread t3 = new Thread(p);
Thread t4 = new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
counter=0;
}
if (ae.getSource() == btn2) {
System.exit(0);
// queue.kongzhi=0;
}
if (ae.getSource() == btn3) {
Work_interface.decrease();
}
if (ae.getSource() == btn4) {
Work_interface.increase();
}
if (ae.getSource() == btn5) {
Work_interface.i();
}
}
}
- 1
- 2
前往页