import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//窗口类
class window extends JFrame{
public JFrame jf;
public JPanel jp3;
public ScrollPane sp1,sp2,sp3;
public Container c;
window(){
jf=new JFrame();
jp3=new JPanel();
sp1=new ScrollPane();
sp2=new ScrollPane();
sp3=new ScrollPane();
c=getContentPane();
c.setLayout(new GridLayout(2,2,10,10));
jf.add(c);
c.add(sp1);
c.add(sp2);
c.add(jp3);
c.add(sp3);
jf.setSize(400,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//Share 类
class Share{
private int u,jishu=0;
private boolean available=false;
public JTextArea jt=new JTextArea("==========生产消费状态==========\n\n");
//同步方法
public synchronized int get(){
jishu++;
while(available==false){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中没有资源,消费者等待……\n");
}
available=false;
notifyAll();
jt.append(jishu+" 正在唤醒生产者生产……\n");
return u;
}
public synchronized void put(int value){
jishu++;
while(available==true){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中已有资源,生产者等待……\n");
}
u=value;
available=true;
notifyAll();
jt.append(jishu+" 正在唤醒消费者消费……\n");
}
//互斥方法
public int hget(){
jishu++;
jt.append(jishu+" 消费者 正在消费资源……\n");
return u;
}
public void hput(int value){
jishu++;
jt.append(jishu+" 生产者 正在生产资源……\n");
u=value;
}
}
//生产者类
class Producer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者进程==========\n\n");
public Producer(Share s){
shared=s;
}
public void run(){
for(int i=1;i<11;i++){
shared.put(i);
jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}
}
}
}
class hProducer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者线程==========\n\n");
public hProducer(Share s){
shared=s;
}
public void run(){
for(int i=1;i<11;i++){
shared.hput(i);
jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}
}
}
}
//消费者类
class Consumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");
public Consumer(Share s){
shared=s;
}
public void run(){
int value=0;
for(int i=1;i<11;i++){
value=shared.get();
jieguo[i]=i+" 消费者第"+i+"次消费"+" 消费者获得的生产数据:"+value+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}
}
}
}
class hConsumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");
public hConsumer(Share s){
shared=s;
}
public void run(){
int value=0;
for(int i=1;i<11;i++){
value=shared.hget();
jieguo[i]=i+" 消费者第"+i+"次消费"+" 消费者获得的生产数据:"+value+"\n";
jt.append(jieguo[i]);
/*try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}*/
}
}
}
//程序入口
public class PCmx{
public static void main(String[] args){
window win=new window();
BHandler h=new BHandler();
BHandler1 h1=new BHandler1();
JButton jb=new JButton("开始同步线程读写");
JButton jb1=new JButton("开始互斥线程读写");
JButton jb2=new JButton("暂停");
win.jf.setTitle("生产者与消费者同步与互斥演示");
jb.addActionListener(h);
jb1.addActionListener(h1);
win.jp3.add(jb);
win.jp3.add(jb1);
h.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
h1.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
}
}
//同步按钮事件
class BHandler implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
jf.setTitle("生产者与消费者的同步与互斥- 同步演示");
Share s=new Share();
Producer p=new Producer(s);
Consumer c=new Consumer(s);
sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}
//互斥按钮事件
class BHandler1 implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
Share s=new Share();
hProducer p=new hProducer(s);
hConsumer c=new hConsumer(s);
sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
jf.setTitle("生产者与消费者的同步与互斥- 互斥演示");
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}