java生产者 消费者问题

  这里涉及了,生产者文件,消费者文件,货物架文件,界面设计文件,还有就是一个applet文件

Consumer文件:

import java.util.*;
public class Consumer extends Thread
{
private WorkQueue queue;
public Consumer(WorkQueue queue)
{
     this.queue=queue;
}
public void run()
{
     for(;;)
     {
      try{
       queue.removeWork();
       sleep(1500);
      }
      catch(InterruptedException e)
      {
       e.printStackTrace();
      }
   
   
     }
}
}

Producer文件

import java.util.*;

public class Producer extends Thread
{
private WorkQueue queue;
public Producer(WorkQueue queue)
{
     this.queue=queue;
}
public void run()
{
     int product=0;
     while(true)
     {
   
      try{
    
       queue.addWork(String.valueOf(++product));
       sleep(1000);
      }
      catch(InterruptedException e)
      {
       e.printStackTrace();
      }
      
     }
}
}
WorkQueue文件:(货物架)

import java.util.*;
import javax.swing.*;
import java.awt.*;
public class WorkQueue
{
final int NUMBER=10;
static String temp="开始了";
static String t="消费了";
int i=0;
MyApplet text;
LinkedList list=new LinkedList();
public WorkQueue()
{
}
public WorkQueue(MyApplet text)
{
     this.text=text;
}
public synchronized void addWork(String product)throws InterruptedException
{
     if(list.size()==NUMBER)
     {
      text.productText.setText("货物架已满");
      try{
       wait();
    
      }
      catch(InterruptedException e)
      {
       e.printStackTrace();
      }
   
     }
     list.addLast(product);

     temp="      ,"+(String)list.get(i++)+temp;

     text.productText.setText(temp);
     if(list.isEmpty())
     {
     }
     notifyAll();
}
public synchronized void removeWork() throws InterruptedException  
{
     while(list.isEmpty())
     {
      text.consumText.setText("没有产品供消费者享用了");
      try{
       wait();
    
      }
      catch(InterruptedException e)
      {
       e.printStackTrace();
      }
     }
  
  
     t="      ,"+(String)list.removeFirst()+t;
     text.consumText.setText(t);
      
  
}
}

MyApplet文件(界面设计)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyApplet extends JApplet implements ActionListener
{
WorkQueue queue=new WorkQueue();

       JButton productButton;
JButton consumButton;
static JTextField productText;
static JTextField consumText;
static JTextField stateText;
public void init()
{
     Container con=getContentPane();
     con.setLayout(new FlowLayout());
     productText=new JTextField(20);
        consumText=new JTextField(20);
     stateText=new JTextField(20);
        productButton=new JButton("Start Product");
        consumButton=new JButton("Start Consum");
     con.add(productButton);
     con.add(productText);
     con.add(consumButton);
     con.add(consumText);
     con.add(stateText);
     productButton.addActionListener(this);
     consumButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
     
     if(e.getSource()==productButton)
     {
         Thread productT=new Producer(queue);
      productT.start();
      stateText.setText("生产者正在生产产品");   
     }
     if(e.getSource()==consumButton)
     {
      new Consumer(queue).start();
      stateText.setText("消费者正在消费产品");
   
     
     }
}

}

JAVA     Applet文件:

<html>
<applet code="MyApplet.class" height="200" width="400"></applet>
</html>

其运行效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值