Java生產者消費者模式例子

 

  1. package ProductConsumer; 
  2.  
  3. import org.ietf.jgss.Oid; 
  4.  
  5.     public class Test { 
  6.         public static void main(String[] args){ 
  7.             Market mt=new Market(); 
  8.             Product product=new Product(mt); 
  9.             Consumer consumer=new Consumer(mt); 
  10.             new Thread(product).start(); 
  11.             new Thread(consumer).start(); 
  12.              
  13.         } 
  14.         
  15.     } 
  16.      
  17.     class Product implements Runnable{ 
  18.         Market market=null
  19.         public Product(Market mt){ 
  20.             this.market=mt; 
  21.         } 
  22.      
  23.     @Override 
  24.     public void run() { 
  25.         // TODO Auto-generated method stub 
  26.         for(int i = 0; i < 20; i++) { 
  27.             WoTou wt=new WoTou(i); 
  28.             market.push(wt); 
  29.             System.out.println("生產了:"+wt); 
  30.             try { 
  31.                 Thread.sleep(1000); 
  32.             } catch (InterruptedException e) { 
  33.                 // TODO Auto-generated catch block 
  34.                 e.printStackTrace(); 
  35.             } 
  36.         } 
  37.       } 
  38.     } 
  39.      
  40.     class Consumer implements Runnable{ 
  41.     Market market=null
  42.     public Consumer(Market mt){ 
  43.         this.market=mt; 
  44.     } 
  45.     @Override 
  46.     public void run() { 
  47.         // TODO Auto-generated method stub 
  48.         for(int i = 0; i < 60; i++) { 
  49.             /*WoTou wt=new WoTou(i); 
  50.             market.push(wt);*/ 
  51.             WoTou wTou=market.pop(); 
  52.             System.out.println("消費了:"+wTou); 
  53.             try { 
  54.                 Thread.sleep(1000); 
  55.             } catch (InterruptedException e) { 
  56.                 // TODO Auto-generated catch block 
  57.                 e.printStackTrace(); 
  58.             } 
  59.         } 
  60.       } 
  61.     } 
  62.      
  63.      
  64.     class Market { 
  65.       WoTou[] wtArr=new WoTou[6]; 
  66.       int index=0
  67.       public synchronized void push(WoTou wt){ 
  68.           wtArr[index]=wt; 
  69.           index++; 
  70.           
  71.          while(index==wtArr.length) { 
  72.             try { 
  73.                 this.wait(); 
  74.             } catch (InterruptedException e) { 
  75.                 // TODO Auto-generated catch block 
  76.                 e.printStackTrace(); 
  77.             } 
  78.         } 
  79.         this.notify();   
  80.       } 
  81.       public synchronized WoTou pop(){ 
  82.           
  83.           while (index==0) { 
  84.             try { 
  85.                 this.wait(); 
  86.             } catch (InterruptedException e) { 
  87.                 // TODO Auto-generated catch block 
  88.                 e.printStackTrace(); 
  89.             } 
  90.         } 
  91.          this.notify(); 
  92.           index--; 
  93.         return wtArr[index]; 
  94.       } 
  95.     } 
  96.      
  97.      
  98.     class WoTou{ 
  99.         int id; 
  100.         public WoTou(int id){ 
  101.             this.id=id; 
  102.         } 
  103.         @Override 
  104.         public String toString() { 
  105.             // TODO Auto-generated method stub 
  106.             return "WOTUO:"+id; 
  107.         } 
  108.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章