线程:生产者与消费者模式

生产者与消费者:

 
  
  1. /** 
  2.  * 生产者每次生产10件商品,消费者每次消费1件商品 
  3.  * 没有商品时生产者生产商品,有商品时消费者消费商品 
  4.  */ 
  5. class Producer extends Thread 
  6.     private Cangku ck; 
  7.      
  8.     public Producer(Cangku ck) 
  9.     { 
  10.         this.ck = ck; 
  11.     } 
  12.      
  13.     public void run() 
  14.     { 
  15.         ck.createProduct(); 
  16.     } 
  17.  
  18. class Consumer extends Thread 
  19.     private Cangku ck; 
  20.      
  21.     public Consumer(Cangku ck) 
  22.     { 
  23.         this.ck = ck; 
  24.     } 
  25.      
  26.     public void run() 
  27.     { 
  28.         ck.useProduct();// 消费商品 
  29.     } 
  30.  
  31. class Cangku//商品对象会变化,创建一个容器对象,锁住不变 
  32.     public synchronized void useProduct()//通过方法进行封装 
  33.     { 
  34.         for (int i = 0; i < 50; i++) 
  35.         { 
  36.             if (productSize() > 0
  37.             { 
  38.                 for (int j = 0; j < 10; j++) 
  39.                 { 
  40.                     Product p = removeProduct(0); 
  41.                     System.out.println("消费了" + j + "个产品"); 
  42.                 } 
  43.             } 
  44.             notify(); 
  45.             try 
  46.             { 
  47.                 if (i != 49
  48.                 { 
  49.                     wait(); 
  50.                 } 
  51.             } 
  52.             catch (InterruptedException e) 
  53.             { 
  54.                 e.printStackTrace(); 
  55.             } 
  56.              
  57.         } 
  58.          
  59.     } 
  60.      
  61.     public synchronized void createProduct() 
  62.     { 
  63.         for (int i = 0; i < 50; i++) 
  64.         { 
  65.             if (productSize() == 0
  66.             { 
  67.                 for (int j = 0; j < 10; j++) 
  68.                 { 
  69.                     addProduct(new Product()); 
  70.                 } 
  71.                 System.out.println("生产了十个产品"); 
  72.                 notify(); 
  73.                 try 
  74.                 { 
  75.                     if (i != 49
  76.                     { 
  77.                         wait(); 
  78.                     } 
  79.                 } 
  80.                 catch (InterruptedException e) 
  81.                 { 
  82.                     e.printStackTrace(); 
  83.                 } 
  84.             } 
  85.         } 
  86.          
  87.     } 
  88.      
  89.     private LinkedList<Product> ppp = new LinkedList<Product>();//创建一个商品类型的容器 
  90.      
  91.     public int productSize() 
  92.     { 
  93.         return ppp.size(); 
  94.     } 
  95.      
  96.     public void addProduct(Product p) 
  97.     { 
  98.         if (null != p) 
  99.         { 
  100.             ppp.add(p); 
  101.         } 
  102.     } 
  103.      
  104.     public Product removeProduct(int index) 
  105.     { 
  106.         return ppp.remove(index); 
  107.     } 
  108.  
  109. class Product 
  110.     String name = "玩具"
  111.  
  112. public class Test 
  113.     public static void main(String[] args) 
  114.     { 
  115.         Cangku ck = new Cangku(); 
  116.          
  117.         Producer p = new Producer(ck); 
  118.         Consumer c = new Consumer(ck); 
  119.         p.start(); 
  120.         c.start(); 
  121.          
  122.     } 

 

重构经过:

1.

 
  
  1. /** 
  2.  * 加油一次20升,每次加完开车10公里 
  3.  * 开车耗完油,没油时加油,有油时开车 
  4.  * 双线程循环五十次 
  5.  */ 
  6. public class TwoThread 
  7.     public static void main(String[] args) 
  8.     { 
  9.         CarOilBox cob = new CarOilBox(); 
  10.         Car c = new Car(cob); 
  11.         Oil o = new Oil(cob); 
  12.         c.start(); 
  13.         o.start(); 
  14.     } 
  15.      
  16.  
  17. class Car extends Thread 
  18.     private CarOilBox cob; 
  19.      
  20.     public Car(CarOilBox cob) 
  21.     { 
  22.         this.cob = cob; 
  23.     } 
  24.      
  25.     @Override 
  26.     public void run() 
  27.     { 
  28.         synchronized (cob) 
  29.         { 
  30.             for (int i = 0; i < 50; i++) 
  31.             { 
  32.                 if (cob.co != null
  33.                 { 
  34.                     for (int j = 0; j < 10; j++) 
  35.                     { 
  36.                         cob.co = null
  37.                         System.out.println("第" + i + "次开车" + j + "公里"); 
  38.                     } 
  39.                 } 
  40.                  
  41.                 cob.notify(); 
  42.                 if (i < 49
  43.                 { 
  44.                     try 
  45.                     { 
  46.                         cob.wait(); 
  47.                     } 
  48.                     catch (InterruptedException e) 
  49.                     { 
  50.                         e.printStackTrace(); 
  51.                     } 
  52.                 } 
  53.             } 
  54.         } 
  55.     } 
  56.  
  57. class Oil extends Thread 
  58.     private CarOilBox cob; 
  59.      
  60.     public Oil(CarOilBox cob) 
  61.     { 
  62.         this.cob = cob; 
  63.     } 
  64.      
  65.     @Override 
  66.     public void run() 
  67.     { 
  68.         synchronized (cob) 
  69.         { 
  70.             for (int i = 0; i < 50; i++) 
  71.             { 
  72.                 if (cob.co == null
  73.                 { 
  74.                     for (int j = 0; j < 20; j++) 
  75.                     { 
  76.                         cob.co = new CarOil(); 
  77.                         System.out.println("第" + i + "次加油" + j + "升"); 
  78.                     } 
  79.                 } 
  80.                  
  81.                 cob.notify(); 
  82.                 if (i < 49
  83.                 { 
  84.                     try 
  85.                     { 
  86.                         cob.wait(); 
  87.                     } 
  88.                     catch (InterruptedException e) 
  89.                     { 
  90.                         e.printStackTrace(); 
  91.                     } 
  92.                 } 
  93.             } 
  94.         } 
  95.     } 
  96.  
  97. class CarOil 
  98.      
  99.  
  100. class CarOilBox 
  101.     CarOil co; 

 

2.

 
  
  1. public class CopyTwoThread 
  2.     public static void main(String[] args) 
  3.     { 
  4.         CarOilBox cob = new CarOilBox(); 
  5.         Car c = new Car(cob); 
  6.         Oil o = new Oil(cob); 
  7.         c.start(); 
  8.         o.start(); 
  9.     } 
  10.      
  11.  
  12. class Car extends Thread 
  13.     private CarOilBox cob; 
  14.      
  15.     public Car(CarOilBox cob) 
  16.     { 
  17.         this.cob = cob; 
  18.     } 
  19.      
  20.     @Override 
  21.     public void run() 
  22.     { 
  23.         synchronized (cob) 
  24.         { 
  25.             for (int i = 0; i < 50; i++) 
  26.             { 
  27.                 if (cob.aco.size() != 0
  28.                 { 
  29.                     for (int j = 0; j < 10; j++) 
  30.                     { 
  31.                         CarOil co1 = cob.aco.remove(0); 
  32.                         CarOil co2 = cob.aco.remove(0); 
  33.                         System.out.println("第" + i + "次开车" + j + "公里"); 
  34.                     } 
  35.                 } 
  36.                  
  37.                 cob.notify(); 
  38.                 if (i < 49
  39.                 { 
  40.                     try 
  41.                     { 
  42.                         cob.wait(); 
  43.                     } 
  44.                     catch (InterruptedException e) 
  45.                     { 
  46.                         e.printStackTrace(); 
  47.                     } 
  48.                 } 
  49.             } 
  50.         } 
  51.     } 
  52.  
  53. class Oil extends Thread 
  54.     private CarOilBox cob; 
  55.      
  56.     public Oil(CarOilBox cob) 
  57.     { 
  58.         this.cob = cob; 
  59.     } 
  60.      
  61.     @Override 
  62.     public void run() 
  63.     { 
  64.         synchronized (cob) 
  65.         { 
  66.             for (int i = 0; i < 50; i++) 
  67.             { 
  68.                 if (cob.aco.size() == 0
  69.                 { 
  70.                     for (int j = 0; j < 20; j++) 
  71.                     { 
  72.                         cob.aco.add(new CarOil()); 
  73.                         System.out.println("第" + i + "次加油" + j + "升"); 
  74.                     } 
  75.                 } 
  76.                  
  77.                 cob.notify(); 
  78.                 if (i < 49
  79.                 { 
  80.                     try 
  81.                     { 
  82.                         cob.wait(); 
  83.                     } 
  84.                     catch (InterruptedException e) 
  85.                     { 
  86.                         e.printStackTrace(); 
  87.                     } 
  88.                 } 
  89.             } 
  90.         } 
  91.     } 
  92.  
  93. class CarOil 
  94.      
  95.  
  96. class CarOilBox 
  97.     CarOil co; 
  98.     ArrayList<CarOil> aco =new ArrayList<CarOil>(); 

 

3.

 
  
  1. public class CopyTwoThread 
  2.     public static void main(String[] args) 
  3.     { 
  4.         CarOilBox cob = new CarOilBox(); 
  5.         Car c = new Car(cob); 
  6.         Oil o = new Oil(cob); 
  7.         c.start(); 
  8.         o.start(); 
  9.     } 
  10.      
  11.  
  12. class Car extends Thread 
  13.     private CarOilBox cob; 
  14.      
  15.     public Car(CarOilBox cob) 
  16.     { 
  17.         this.cob = cob; 
  18.     } 
  19.      
  20.     @Override 
  21.     public void run() 
  22.     { 
  23.         synchronized (cob) 
  24.         { 
  25.             for (int i = 0; i < 50; i++) 
  26.             { 
  27.                 if (cob.arraySize() != 0
  28.                 { 
  29.                     for (int j = 0; j < 10; j++) 
  30.                     { 
  31.                         CarOil co1 = cob.arrayRemove(); 
  32.                         CarOil co2 = cob.arrayRemove(); 
  33.                         System.out.println("第" + i + "次开车" + j + "公里"); 
  34.                     } 
  35.                 } 
  36.                  
  37.                 cob.notify(); 
  38.                 if (i < 49
  39.                 { 
  40.                     try 
  41.                     { 
  42.                         cob.wait(); 
  43.                     } 
  44.                     catch (InterruptedException e) 
  45.                     { 
  46.                         e.printStackTrace(); 
  47.                     } 
  48.                 } 
  49.             } 
  50.         } 
  51.     } 
  52.  
  53. class Oil extends Thread 
  54.     private CarOilBox cob; 
  55.      
  56.     public Oil(CarOilBox cob) 
  57.     { 
  58.         this.cob = cob; 
  59.     } 
  60.      
  61.     @Override 
  62.     public void run() 
  63.     { 
  64.         synchronized (cob) 
  65.         { 
  66.             for (int i = 0; i < 50; i++) 
  67.             { 
  68.                 if (cob.arraySize() == 0
  69.                 { 
  70.                     for (int j = 0; j < 20; j++) 
  71.                     { 
  72.                         cob.arrayAdd(); 
  73.                         System.out.println("第" + i + "次加油" + j + "升"); 
  74.                     } 
  75.                 } 
  76.                  
  77.                 cob.notify(); 
  78.                 if (i < 49
  79.                 { 
  80.                     try 
  81.                     { 
  82.                         cob.wait(); 
  83.                     } 
  84.                     catch (InterruptedException e) 
  85.                     { 
  86.                         e.printStackTrace(); 
  87.                     } 
  88.                 } 
  89.             } 
  90.         } 
  91.     } 
  92.  
  93. class CarOil 
  94.      
  95.  
  96. class CarOilBox 
  97.     //CarOil co; 
  98.     private ArrayList<CarOil> aco =new ArrayList<CarOil>(); 
  99.      
  100.     public int arraySize() 
  101.     { 
  102.         return aco.size(); 
  103.     } 
  104.      
  105.     public CarOil arrayRemove() 
  106.     { 
  107.         return aco.remove(0); 
  108.     } 
  109.      
  110.     public void arrayAdd() 
  111.     { 
  112.         aco.add(new CarOil()); 
  113.     } 

 

4.

 
  
  1. public class CopyTwoThread 
  2.     public static void main(String[] args) 
  3.     { 
  4.         CarOilBox cob = new CarOilBox(); 
  5.         Car c = new Car(cob); 
  6.         Oil o = new Oil(cob); 
  7.         c.start(); 
  8.         o.start(); 
  9.     } 
  10.      
  11.  
  12. class Car extends Thread 
  13.     private CarOilBox cob; 
  14.      
  15.     public Car(CarOilBox cob) 
  16.     { 
  17.         this.cob = cob; 
  18.     } 
  19.      
  20.     @Override 
  21.     public void run() 
  22.     { 
  23.         cob.driveCar(cob); 
  24.     } 
  25.  
  26. class Oil extends Thread 
  27.     private CarOilBox cob; 
  28.      
  29.     public Oil(CarOilBox cob) 
  30.     { 
  31.         this.cob = cob; 
  32.     } 
  33.      
  34.     @Override 
  35.     public void run() 
  36.     { 
  37.         cob.addOil(cob); 
  38.     } 
  39.  
  40. class CarOil 
  41.      
  42.  
  43. class CarOilBox 
  44.     //CarOil co; 
  45.     private ArrayList<CarOil> aco =new ArrayList<CarOil>(); 
  46.      
  47.     public int arraySize() 
  48.     { 
  49.         return aco.size(); 
  50.     } 
  51.      
  52.     public CarOil arrayRemove() 
  53.     { 
  54.         return aco.remove(0); 
  55.     } 
  56.      
  57.     public void arrayAdd() 
  58.     { 
  59.         aco.add(new CarOil()); 
  60.     } 
  61.     public synchronized void driveCar(CarOilBox cob) 
  62.     { 
  63.  
  64.         for (int i = 0; i < 50; i++) 
  65.         { 
  66.             if (arraySize() != 0
  67.             { 
  68.                 for (int j = 0; j < 10; j++) 
  69.                 { 
  70.                     CarOil co1 = arrayRemove(); 
  71.                     CarOil co2 = arrayRemove(); 
  72.                     System.out.println("第" + i + "次开车" + j + "公里"); 
  73.                 } 
  74.             } 
  75.              
  76.             notify(); 
  77.             if (i < 49
  78.             { 
  79.                 try 
  80.                 { 
  81.                     wait(); 
  82.                 } 
  83.                 catch (InterruptedException e) 
  84.                 { 
  85.                     e.printStackTrace(); 
  86.                 } 
  87.             } 
  88.         } 
  89.      
  90.     } 
  91.      
  92.     public synchronized void addOil(CarOilBox cob) 
  93.     { 
  94.  
  95.         for (int i = 0; i < 50; i++) 
  96.         { 
  97.             if (arraySize() == 0
  98.             { 
  99.                 for (int j = 0; j < 20; j++) 
  100.                 { 
  101.                     arrayAdd(); 
  102.                     System.out.println("第" + i + "次加油" + j + "升"); 
  103.                 } 
  104.             } 
  105.              
  106.             notify(); 
  107.             if (i < 49
  108.             { 
  109.                 try 
  110.                 { 
  111.                     wait(); 
  112.                 } 
  113.                 catch (InterruptedException e) 
  114.                 { 
  115.                     e.printStackTrace(); 
  116.                 } 
  117.             } 
  118.         } 
  119.        
  120.     } 




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1195004,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值