`
qinya06
  • 浏览: 600511 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

生产者 消费者

阅读更多
public class Consumer extends Thread {   
    private ProductTable productTable;   
  
    public Consumer(ProductTable productTable) {   
        this.productTable = productTable;   
    }   
  
    public void run() {   
        for (int i = 1; i <= 10; i++) {   
            try {   
                // wait for a random time   
                Thread.sleep((int) (Math.random() * 3000));   
            }   
            catch (InterruptedException e) {   
                e.printStackTrace();   
            }   
            productTable.getProduct();   
        }   
    }   
}  


public class Producer extends Thread {   
    private ProductTable productTable;   
    private Product p;   
  
    public Producer(ProductTable productTable, Product p) {   
        this.productTable = productTable;   
        this.p = p;   
    }   
  
    public void run() {   
        for (int product = 1; product <= 10; product++) {   
            productTable.addProduct(p);   
        }   
    }   
}  


public class ProductTable {      
    private LinkedList products = new LinkedList();      
     
    public synchronized void addProduct(Product product) {      
        while(products.size() >= 2) { // 容量限制為 2      
            try {      
                wait();      
            }      
            catch(InterruptedException e) {}      
        }      
     
        products.addLast(product);      
        System.out.println(product.getId()+" added");      
        notifyAll();      
    }      
     
 public synchronized Product[] getProducts() {      
        while(products.size() <= 0) {      
            try {      
                wait();      
            }      
            catch(InterruptedException e) {}      
        }      
    
        // remove two products every time   
        Product[] products= new Product[2];   
        products[0] = (Product) products.removeFirst();      
        System.out.println(products[0].getId()+" removed");     
        products[1]= (Product) products.removeFirst();   
        System.out.println(products[1].getId()+" removed");    
  
        // notify all the producers waiting for producing   
        notifyAll();      
     
        return products;      
    }   


public class Main {      
     
    public static void main(String[] args) {      
        Product a = new Product(1, "a");      
        Product b = new Product(2, "b");      
        ProductTable queue = new ProductTable();    
     
        for(int i=0; i<10; i++) {   
             Producer producerA = new Producer(queue, a);    
             Producer producerB = new Producer(queue, b);    
             Consumer consumer = new Consumer(queue);      
                
             new Thread(producerA).start();   
             new Thread(producerB).start();      
             new Thread(consumer).start();   
      
    }      
} 
  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics