Producter and Consumer

  1 package pinx.thread;
  2 
  3 import java.util.LinkedList;
  4 import java.util.Queue;
  5 
  6 public class ProducerConsumer {
  7 
  8     Storage storage=null;
  9     Query query=null;
 10     public ProducerConsumer(int max){
 11         storage=new Storage(max);
 12         query=new Query();
 13     }
 14     
 15     public void start(){
 16         Producter producter=new Producter();
 17         Consumer consumer=new Consumer();
 18         Thread thread1=new Thread(producter);
 19         
 20         Thread thread2=new Thread(consumer);
 21         Thread thread3=new Thread(consumer);
 22         Thread thread4=new Thread(consumer);
 23         
 24         thread1.start();
 25         thread2.start();
 26         thread3.start();
 27         thread4.start();
 28     }
 29     
 30     /**
 31      * @param args
 32      */
 33     public static void main(String[] args) {
 34         ProducerConsumer pc=new ProducerConsumer(20);
 35         pc.start();
 36     }
 37 
 38     class Product {
 39         private int id;
 40 
 41         public Product(int id) {
 42             this.id = id;
 43         }
 44 
 45         public String toString() {
 46             return "Product " + this.id;
 47         }
 48     }
 49     
 50     class Query{
 51         public Query(){}
 52         public synchronized int get(){
 53             return -1;
 54         }
 55     }
 56 
 57     class Storage {
 58         Queue<Product> queue = null;
 59         int max = 0;
 60 
 61         public Storage(int max) {
 62             this.max = max;
 63             this.queue = new LinkedList<Product>();
 64         }
 65 
 66         public synchronized void push(Product product) {
 67             while (this.max == this.queue.size()) {
 68                 try {
 69                     this.wait();
 70                 } catch (InterruptedException e) {
 71                     e.printStackTrace();
 72                 }
 73             }
 74             this.queue.offer(product);
 75             System.out.println("Producter has generated a product :"
 76                     + product.toString());
 77             this.notifyAll();
 78         }
 79 
 80         public synchronized Product pop() {
 81             while (this.queue.isEmpty()) {
 82                 try {
 83                     this.wait();
 84                 } catch (InterruptedException e) {
 85                     e.printStackTrace();
 86                 }
 87             }
 88             Product product = this.queue.poll();
 89             System.out.println("Consumer has consumed a product :"
 90                     + product.toString()+" ~~~");
 91             return product;
 92         }
 93     }
 94 
 95     class Consumer implements Runnable {
 96 
 97         public void run() {
 98             while(true){
 99                 System.out.println(String.format("Producter get the query value : %d", query.get()));
100                 storage.pop();
101                 try {
102                     Thread.sleep((int)(Math.random()*1000));
103                 } catch (InterruptedException e) {
104                     e.printStackTrace();
105                 }
106             }
107         }
108     }
109 
110     class Producter implements Runnable {
111 
112         public void run() {
113             int index=0;
114             while(true){
115                 System.out.println(String.format("Producter get the query value : %d~~~~~~~~~~~~~~~~~~~", query.get()));
116                 storage.push(new Product(++index));
117                 try {
118                     Thread.sleep((int)(Math.random()*1000));
119                 } catch (InterruptedException e) {
120                     e.printStackTrace();
121                 }
122             }
123         }
124     }
125 
126 }
posted @ 2014-07-17 23:30  PinXiong  阅读(345)  评论(0编辑  收藏  举报