java生产者消费者问题实现

来源:互联网 发布:华为路由器nat端口复用 编辑:程序博客网 时间:2024/05/16 09:37

  1.   
  2. /** 
  3.  *经典生产者与消费者问题:生产者不断的往仓库中存放产品,消费者从仓库中消费产品。 
  4.  *其中生产者和消费者都可以有若干个。仓库容量有限,库满时不能存放,库空时不能取产品  
  5.  */  
  6.   
  7. public class ProducersAndConsumers {  
  8.     public static void main(String[] args) {  
  9.         Storage storage = new Storage();  
  10.         Thread consumer = new Thread(new Consumer(storage));  
  11.         consumer.setName("消费者");  
  12.         Thread producer = new Thread(new Producer(storage));  
  13.         producer.setName("生产者");  
  14.         consumer.start();  
  15.         producer.start();  
  16.     }  
  17. }  
  18.   
  19. /** 
  20.  * 消费者 
  21.  */  
  22. class Consumer implements Runnable {  
  23.     private Storage storage;  
  24.   
  25.     public Consumer(Storage storage) {  
  26.         this.storage = storage;  
  27.     }  
  28.   
  29.     @Override  
  30.     public void run() {  
  31.         storage.pop();  
  32.     }  
  33. }  
  34.   
  35. /** 
  36.  * 生产者 
  37.  */  
  38. class Producer implements Runnable {  
  39.     private Storage storage;  
  40.   
  41.     public Producer(Storage storage) {  
  42.         this.storage = storage;  
  43.     }  
  44.   
  45.     @Override  
  46.     public void run() {  
  47.         Product product = new Product("090505105""电话");  
  48.         storage.push(product);  
  49.     }  
  50.   
  51. }  
  52.   
  53. /** 
  54.  * 产品类 
  55.  */  
  56. class Product {  
  57.     private String id;// 产品id  
  58.     private String name;// 产品名称  
  59.   
  60.     public Product(String id, String name) {  
  61.         this.id = id;  
  62.         this.name = name;  
  63.     }  
  64.   
  65.     @Override  
  66.     public String toString() {  
  67.         return "(产品ID:" + id + " 产品名称:" + name + ")";  
  68.     }  
  69.   
  70.     public String getId() {  
  71.         return id;  
  72.     }  
  73.   
  74.     public void setId(String id) {  
  75.         this.id = id;  
  76.     }  
  77.   
  78.     public String getName() {  
  79.         return name;  
  80.     }  
  81.   
  82.     public void setName(String name) {  
  83.         this.name = name;  
  84.     }  
  85.   
  86. }  
  87.   
  88. /** 
  89.  *仓库 
  90.  */  
  91. class Storage {  
  92.     // 仓库容量为10  
  93.     private Product[] products = new Product[10];  
  94.     private int top = 0;  
  95.   
  96.     // 生产者往仓库中放入产品  
  97.     public synchronized void push(Product product) {  
  98.         while (top == products.length) {  
  99.             try {  
  100.                 wait();//仓库已满,等待  
  101.             } catch (InterruptedException e) {  
  102.                 // TODO Auto-generated catch block  
  103.                 e.printStackTrace();  
  104.             }  
  105.         }  
  106.         //把产品放入仓库  
  107.         products[top++] = product;  
  108.         System.out.println(Thread.currentThread().getName() + " 生产了产品"  
  109.                 + product);  
  110.         notifyAll();//唤醒等待线程  
  111.   
  112.     }  
  113.   
  114.     // 消费者从仓库中取出产品  
  115.     public synchronized Product pop() {  
  116.         while (top == 0) {  
  117.             try {  
  118.                 wait();//仓库空,等待  
  119.             } catch (InterruptedException e) {  
  120.                 // TODO Auto-generated catch block  
  121.                 e.printStackTrace();  
  122.             }  
  123.   
  124.         }  
  125.   
  126.         //从仓库中取产品  
  127.         --top;  
  128.         Product p = new Product(products[top].getId(), products[top].getName());  
  129.         products[top] = null;  
  130.         System.out.println(Thread.currentThread().getName() + " 消费了产品" + p);  
  131.         notifyAll();//唤醒等待线程  
  132.         return p;  
  133.     }  
  134. }  
原创粉丝点击