Java多线程同步——生产者消费者问题

来源:互联网 发布:遇到网络诈骗怎么半 编辑:程序博客网 时间:2024/06/04 18:34

这是马士兵老师的Java视频教程里的一个生产者消费者问题的模型

[java] view plaincopy
  1. public class ProduceConsumer{  
  2.     public static void main(String[] args){  
  3.         SyncStack ss = new SyncStack();  
  4.         Producer pro = new Producer(ss);  
  5.         Consumer con = new Consumer(ss);  
  6.         new Thread(pro).start();  
  7.         new Thread(con).start();  
  8.           
  9.     }     
  10. }  
  11.   
  12. class Product{  
  13.     int id;  
  14.     public Product(int id){  
  15.         this.id = id;  
  16.     }     
  17.     public String toString(){  
  18.         return "Product:" + id;  
  19.     }  
  20. }  
  21.   
  22. class SyncStack{  
  23.     int index  = 0;  
  24.     Product[] arrPro = new Product[6];  
  25.       
  26.     public synchronized void push(Product p){  
  27.         while (index == arrPro.length){  
  28.             try {  
  29.                 this.wait();  
  30.             } catch (InterruptedException e) {  
  31.                 // TODO Auto-generated catch block  
  32.                 e.printStackTrace();  
  33.             }  
  34.         }  
  35.           
  36.         this.notify();        
  37.         arrPro[index] = p;  
  38.         index++;  
  39.     }  
  40.       
  41.     public synchronized Product pop(){  
  42.         while (index == 0){  
  43.             try {  
  44.                 this.wait();  
  45.             } catch (InterruptedException e) {  
  46.                 // TODO Auto-generated catch block  
  47.                 e.printStackTrace();  
  48.             }  
  49.         }  
  50.           
  51.         this.notify();    
  52.         index--;  
  53.         return arrPro[index];  
  54.     }  
  55.       
  56. }  
  57.   
  58. class Producer implements Runnable{  
  59.     SyncStack ss = null;  
  60.     public Producer(SyncStack ss){ //持有SyncStack的一个引用  
  61.         this.ss = ss;  
  62.     }  
  63.     @Override  
  64.     public void run() {  
  65.         for(int i=0; i<20; i++){  
  66.             Product p = new Product(i);  
  67.             ss.push(p);  
  68. System.out.println("生产了:" + p);  
  69.             try {  
  70.                 Thread.sleep(100);  
  71.             } catch (InterruptedException e) {  
  72.                 e.printStackTrace();  
  73.             }  
  74.           
  75.         }  
  76.           
  77.     }     
  78. }  
  79.   
  80. class Consumer implements Runnable{  
  81.   
  82.     SyncStack ss = null;  
  83.     public Consumer(SyncStack ss){ //持有SyncStack的一个引用  
  84.         this.ss = ss;  
  85.     }  
  86.     @Override  
  87.     public void run() {  
  88.         for(int i=0; i<20; i++){  
  89.             Product p = ss.pop();  
  90. System.out.println("消费了:" + p);  
  91.             try {  
  92.                 Thread.sleep(1000);  
  93.             } catch (InterruptedException e) {  
  94.                 e.printStackTrace();  
  95.             }  
  96.           
  97.         }  
  98.           
  99.     }     
  100. }  


0 0