生产者-消费者实例

  1. package com.rx.thread.produce.consume;
  2. public class Product {
  3.     
  4.     private String id;
  5.     public String getId() {
  6.         return id;
  7.     }
  8.     public void setId(String id) {
  9.         this.id = id;
  10.     }
  11.     public Product(String id) {
  12.         this.id = id;
  13.     }
  14.     
  15.     public String toString() {
  16.         return this.id;
  17.     }
  18.     
  19.     public static void main(String[] args) {
  20.         System.out.println(new Product("mrl"));
  21.     }
  22. }
  1. package com.rx.thread.produce.consume;
  2. public class Store {
  3.     public Store() {    
  4.     }
  5.     
  6.     /** 仓库中的产品 */
  7.     private Product product = null;
  8.     
  9.     public Product getProduct() {
  10.         return product;
  11.     }
  12.     public void setProduct(Product product) {
  13.         this.product = product;
  14.     }
  15.     /** 显示当前仓库状态是否满 */
  16.     private boolean empty = true;
  17.     /***
  18.      * 放置产品
  19.      * @param product 放置的产品,可以抽象出来,放置任意物品
  20.      */
  21.     public void putProduct(Product product) {
  22.         // 同步对象锁
  23.         synchronized(this ) {
  24.             while(!empty) {
  25.                 try {
  26.                     System.out.println(Thread.currentThread().getName() + " now is waiting for consuming up");
  27.                     // 释放锁
  28.                     this.wait(1000);
  29.                 } catch (InterruptedException e) {
  30.                     System.err.print(e.toString());
  31.                     e.printStackTrace();
  32.                 }
  33.             }
  34.             
  35.             empty = false;
  36.             this.product = product;
  37.             System.out.println(Thread.currentThread().getName() + " put product : " + product.getId());
  38.             
  39.             //notify();
  40.             this.notifyAll();
  41.         }
  42.     }
  43.     
  44.     /***
  45.      * 取出产品
  46.      * @return 取出的产品
  47.      * @throws InterruptedException 
  48.      */
  49.     public synchronized Product takeOutProduct()  {
  50.         while(empty){
  51.             try {
  52.                 System.out.println(Thread.currentThread().getName() + " now is waiting for product");
  53.                 // 释放锁,并从该处返回
  54.                 this.wait(1000);
  55.                 // 获得权限(notify)后继续执行
  56.                 System.out.println("waiting....");
  57.             } catch (InterruptedException e) {
  58.                 System.err.print(e.toString());
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.             
  63.         System.out.println(Thread.currentThread().getName() + " take out product : "+ product.getId());
  64.         empty = true;
  65.         //notify();
  66.         this.notifyAll();
  67.         return this.product;
  68.     }
  69. }

 

  1. package com.rx.thread.produce.consume;
  2. public class Produce implements Runnable {
  3.     /** 仓库 */
  4.     Store store;
  5.     
  6.     /***
  7.      * 设置产品放置的仓库
  8.      * @param store 仓库
  9.      */
  10.     public Produce(Store store) {
  11.         this.store = store;
  12.     }
  13.     
  14.     public void run() {
  15.         for(int i=0; i<4; i++) {
  16.             store.putProduct(new Product(String.valueOf(i)));
  17.             try {
  18.                 Thread.sleep(1000);
  19.             } catch (InterruptedException e) {
  20.                 
  21.                 e.printStackTrace();
  22.             }
  23.         }
  24.     }
  25. }

 

  1. package com.rx.thread.produce.consume;
  2. public class Consume implements Runnable {
  3.     
  4.     /** 仓库 */
  5.     Store store;
  6.     
  7.     /***
  8.      * 设置消费的仓库
  9.      * @param store 仓库对象
  10.      */
  11.     public Consume(Store store) {
  12.         this.store = store;
  13.     }
  14.     
  15.     public void run() {
  16.         while(true) {
  17.             if(store.takeOutProduct().getId().endsWith("3")) {
  18.                 break;
  19.             }
  20.         }
  21.         System.out.println("end.");
  22.     }
  23. }
  1. package com.rx.thread.produce.consume;
  2. public class Test {
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         
  8.         Store store = new Store();
  9.         
  10.         (new Thread(new Produce(store))).start();
  11.         //(new Thread(new Produce(store))).start();
  12.         (new Thread(new Consume(store))).start();
  13.         //(new Thread(new Consume(store))).start();
  14.     }
  15. }

 

Thread-0 put product : 0
Thread-1 take out product : 0
Thread-1 now is waiting for product
Thread-0 put product : 1
waiting....
Thread-1 take out product : 1
Thread-1 now is waiting for product
waiting....
Thread-1 now is waiting for product
Thread-0 put product : 2
waiting....
Thread-1 take out product : 2
Thread-1 now is waiting for product
waiting....
Thread-1 now is waiting for product
Thread-0 put product : 3
waiting....
Thread-1 take out product : 3
end.

 

你可能感兴趣的:(生产者-消费者实例)