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>

其運行效果如下:

d9c0d7cb14376ffa53664f6a.jpg
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章