生產者消費者之爸爸媽媽兒子女兒蘋果橘子編程實現

生產者消費者之爸爸媽媽兒子女兒蘋果橘子編程實現

桌上有一個空盤子,只容許放一個水果。爸爸能夠向盤中放蘋果,也能夠向盤中放桔子,兒子專等吃盤中的桔子,女兒專等吃盤中的蘋果。規定當盤空時,一次只能放一隻水果。

下面是程序的具體實現代碼,在寫這個程序的時候,有點小問題,糾結了很長時間,因而在csdn論壇上發表帖子終於獲得瞭解決html

先說說涉及到的類的做用,首先Fruits做爲一個水果的父類,Apple和Orange是Fruits類的擴展類。CriticalResources類是臨界資源類,做爲緩衝區用,裏面封裝了數組大小爲一的Fruits數組,能夠當作「盤子」;ProducerOrange爲生產橘子的類 ProducerApple爲生產桔子的類 ConsumerApple(消費蘋果的類) ConsumerOrange(消費桔子的類)java

水果類代碼以下編程

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. public class Fruits {  
  4.     private String name;  
  5.   
  6.     public Fruits(String name) {  
  7.         this.name = name;  
  8.     }  
  9.   
  10.     @Override  
  11.     public String toString() {  
  12.         return name;  
  13.     }  
  14.   
  15.     public Fruits() {  
  16.         super();  
  17.     }  
  18.   
  19. }  

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. /** 
  4.  * @author zhuyong  
  5.  * 建立時間:2012-6-6 上午09:46:14 
  6.  * 類說明 : 
  7.  */  
  8. public class Orange extends Fruits {  
  9.   
  10.     public Orange() {  
  11.         super();  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.   
  15.     public Orange(String name) {  
  16.         super(name);  
  17.         // TODO Auto-generated constructor stub  
  18.     }  
  19.       
  20. }  
[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. /** 
  4.  * @author zhuyong  
  5.  * 建立時間:2012-6-6 上午09:46:14 
  6.  */  
  7. public class Apple extends Fruits {  
  8.   
  9.     public Apple() {  
  10.         super();  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.   
  14.     public Apple(String name) {  
  15.         super(name);  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.       
  19. }  


下面是做爲緩衝區盤子的代碼數組

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. public class CriticalResources {  
  4.     private int index = 0;  
  5.     private static Fruits[] fruites = new Fruits[1];// 默認臨界區有五個商品  
  6.     private ProducerApple pa = new ProducerApple(this);  
  7.     private ProducerOrange po = new ProducerOrange(this);  
  8.     private ConsumerApple ca = new ConsumerApple(this);  
  9.     private ConsumerOrange co = new ConsumerOrange(this);  
  10.   
  11.     /** 
  12.      * 向臨界資源裏添加商品 利用synchronized關鍵字實現同步 添加後數組指針+1 
  13.      * 若是臨界資源數組裏面裝滿了商品不能再生產,則生產線程等待以便讓消費者消費 
  14.      *  
  15.      * @param goods 
  16.      */  
  17.     public synchronized void push(Fruits goods) {  
  18.         //  
  19.         while (this.index == this.fruites.length) {  
  20.             try {  
  21.                 this.wait();  
  22.             } catch (InterruptedException e) {  
  23.                 // TODO Auto-generated catch block  
  24.                 e.printStackTrace();  
  25.             }  
  26.         }  
  27.   
  28.         this.notifyAll();// 喚醒生產者  
  29.         this.fruites[index++] = goods;  
  30.   
  31.     }  
  32.   
  33.     /** 
  34.      * 從臨界資源裏拿商品,先減一後返回 若是index==0說明, 臨界資源數組裏面沒有商品,不能在消費 消費線程阻塞, 讓生產者生產 則讓生產者 
  35.      *  
  36.      * @return 
  37.      */  
  38.     public synchronized Fruits pop() {  
  39.         while (this.index == 0) {  
  40.             try {  
  41.                 this.wait();  
  42.             } catch (InterruptedException e) {  
  43.                 // TODO Auto-generated catch block  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.   
  48.         this.notifyAll();// 喚醒消費者  
  49.         return fruites[--index];  
  50.   
  51.     }  
  52.   
  53.     /** 
  54.      * 看看是否是空了 
  55.      *  
  56.      * @return 
  57.      */  
  58.     public synchronized Fruits peek() {  
  59.         if (this.index == 0)  
  60.             return null;  
  61.         else  
  62.             return fruites[index - 1];  
  63.     }  
  64.   
  65.     public int getIndex() {  
  66.         return index;  
  67.     }  
  68.   
  69.     public void setIndex(int index) {  
  70.         this.index = index;  
  71.     }  
  72.   
  73.     public Fruits[] getFruites() {  
  74.         return fruites;  
  75.     }  
  76.   
  77. }  


下面是生產桔子和消費桔子的代碼app

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. public class ProducerOrange implements Runnable {  
  4.     CriticalResources cr = null;  
  5.   
  6.     public ProducerOrange(CriticalResources cr) {  
  7.         super();  
  8.         this.cr = cr;  
  9.     }  
  10.     int count = 5; //作5次  
  11.   
  12.     @Override  
  13.     public void run() {  
  14.          while(count-->0)  
  15.                 synchronized (cr) {  
  16.                     while (cr.peek() != null) {  
  17.                         try {  
  18.                             cr.wait();// 該生產這等待  
  19.                         } catch (InterruptedException e) {  
  20.                             // TODO Auto-generated catch block  
  21.                             e.printStackTrace();  
  22.                         }  
  23.                     }  
  24.                     Fruits fruits = new Orange("橘子");  
  25.                     cr.push(fruits);  
  26.                     System.out.println("橘子生產商生產了" + fruits);  
  27.                     cr.notifyAll();  
  28.                 }  
  29.   
  30.     }  
  31.   
  32. }  


 

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. /** 
  4.  * 消費橘子的消費者 
  5.  *  
  6.  * @author Administrator 
  7.  *  
  8.  */  
  9. public class ConsumerOrange implements Runnable {  
  10.   
  11.     private CriticalResources cr = null;// 封裝一個臨界資源對象,以便消費  
  12.   
  13.     public ConsumerOrange(CriticalResources cr) {  
  14.         super();  
  15.         this.cr = cr;  
  16.     }  
  17.   
  18.     int count = 5;  
  19.   
  20.     @Override  
  21.     public void run() {  
  22.   
  23.         while (count-- > 0)  
  24.             // 若是緩衝區是蘋果  
  25.             synchronized (cr) {  
  26.                 while (!(cr.peek() instanceof Orange)) {  
  27.                     try {  
  28.                         cr.wait();// 該消費者等待  
  29.                     } catch (InterruptedException e) {  
  30.                         // TODO Auto-generated catch block  
  31.                         e.printStackTrace();  
  32.                     }  
  33.                 }  
  34.                 Fruits fruits = cr.pop();  
  35.   
  36.                 System.out.println("----橘子消費者消費了-------" + fruits);  
  37.                 cr.notifyAll();  
  38.             }  
  39.   
  40.     }  
  41.   
  42. }  


下面是生產蘋果核消費蘋果的代碼ide

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3.   
  4. /** 
  5.  * 生產蘋果的生產者 
  6.  *  
  7.  * @author Arthur 
  8.  *  
  9.  */  
  10. public class ProducerApple implements Runnable {  
  11.     private CriticalResources cr = null;// 封裝一個臨界資源對象,以便生產  
  12.   
  13.     public ProducerApple(CriticalResources cr) {  
  14.         super();  
  15.         this.cr = cr;  
  16.     }  
  17.   
  18.     private int count = 5;  
  19.   
  20.     @Override  
  21.     public void run() {  
  22.         while (count-- > 0)  
  23.             synchronized (cr) {  
  24.                 while ((cr.peek() != null)) {  
  25.                     try {  
  26.                         cr.wait();// 緩衝區滿,該生產者等待  
  27.                     } catch (InterruptedException e) {  
  28.                         e.printStackTrace();  
  29.                     }  
  30.                 }  
  31.                 //若是不加鎖,此處容易被打斷  
  32.                 Fruits fruits = new Apple("蘋果");  
  33.                 cr.push(fruits);  
  34.                 System.out.println("蘋果生產商生產了" + fruits);  
  35.                 cr.notifyAll();  
  36.             }  
  37.   
  38.     }  
  39.   
  40. }  


 

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. /** 
  4.  * 消費蘋果的消費者 
  5.  *  
  6.  * @author Arthur 
  7.  *  
  8.  */  
  9. public class ConsumerApple implements Runnable {  
  10.     private CriticalResources cr = null;// 封裝一個臨界資源對象,以便消費  
  11.   
  12.     public ConsumerApple(CriticalResources cr) {  
  13.         super();  
  14.         this.cr = cr;  
  15.     }  
  16. int count = 5;  
  17.     @Override  
  18.     public void run() {  
  19.          while(count-->0)  
  20.                 synchronized (cr) {  
  21.                     while (!(cr.peek() instanceof Apple) ) {  
  22.                         try {  
  23.                             cr.wait();  
  24.                         } catch (InterruptedException e) {  
  25.                             e.printStackTrace();  
  26.                         }  
  27.                     }  
  28.                     Fruits fruits = cr.pop();  
  29.   
  30.                     System.out.println("----蘋果消費者消費了-------" + fruits);  
  31.                     cr.notifyAll();  
  32.                       
  33.                 }  
  34.   
  35.   
  36.     }  
  37. }  


客戶端代碼post

[java]  view plain  copy
 
  1. package com.bankht.producerCustomer;  
  2.   
  3. public class Client {  
  4.   
  5.       /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         CriticalResources cr = new CriticalResources();  
  10.         // 生產蘋果實例  
  11.         ProducerApple appleP = new ProducerApple(cr);  
  12.         // 消費蘋果實例  
  13.         ConsumerApple appleC = new ConsumerApple(cr);  
  14.   
  15.         // 橘子生產者實例  
  16.         ProducerOrange orangeP = new ProducerOrange(cr);  
  17.         // 橘子消費者實例  
  18.         ConsumerOrange orangeC = new ConsumerOrange(cr);  
  19.         // 生產蘋果線程  
  20.         Thread pThread = new Thread(appleP);  
  21.         // 消費蘋果線程  
  22.         Thread cThread = new Thread(appleC);  
  23.         // 生產橘子線程  
  24.         Thread pt = new Thread(orangeP);  
  25.         // 消費橘子線程  
  26.         Thread ct = new Thread(orangeC);  
  27.   
  28.          
  29.   
  30.         pThread.start();  
  31.         cThread.start();  
  32.         pt.start();  
  33.         ct.start();  
  34.     }  
  35.   
  36. }  


運行結果:ui

 

[html]  view plain  copy
 
  1. 蘋果生產商生產了蘋果  
  2. ----蘋果消費者消費了-------蘋果  
  3. 橘子生產商生產了橘子  
  4. ----橘子消費者消費了-------橘子  
  5. 蘋果生產商生產了蘋果  
  6. ----蘋果消費者消費了-------蘋果  
  7. 橘子生產商生產了橘子  
  8. ----橘子消費者消費了-------橘子  
  9. 蘋果生產商生產了蘋果  
  10. ----蘋果消費者消費了-------蘋果  
  11. 橘子生產商生產了橘子  
  12. ----橘子消費者消費了-------橘子  
  13. 蘋果生產商生產了蘋果  
  14. ----蘋果消費者消費了-------蘋果  
  15. 橘子生產商生產了橘子  
  16. ----橘子消費者消費了-------橘子  
  17. 蘋果生產商生產了蘋果  
  18. ----蘋果消費者消費了-------蘋果  
  19. 橘子生產商生產了橘子  
  20. ----橘子消費者消費了-------橘子  
相關文章
相關標籤/搜索