`

java多线程总结六:经典生产者消费者问题实现

阅读更多

这是个线程同步的经典例子,源代码如下:

 

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


 

 

运行结果:

生产者 生产了产品(产品ID:010435111 产品名称:电话)
消费者 消费了产品(产品ID:010435111 产品名称:电话)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics