多線程 之 生產者與消費者

public class Consumer extends Thread {
    // 每次消費的產品數量
    private int num;
 
    // 所在放置的倉庫
    private Storage storage;
 
    // 構造函數,設置倉庫
    public Consumer(Storage storage) {
        this.storage = storage;
    }
 
    // 線程run函數
    public void run() {
        consume(num);
    }
 
    // 調用倉庫Storage的生產函數
    public void consume(int num) {
        storage.consume(num);
    }
 
    // get/set方法
    public int getNum() {
        return num;
    }
 
    public void setNum(int num) {
        this.num = num;
    }
 
    public Storage getStorage() {
        return storage;
    }
 
    public void setStorage(Storage storage) {
        this.storage = storage;
    }
}
 
public class Producer extends Thread {
    // 每次生產的產品數量
    private int num;
 
    // 所在放置的倉庫
    private Storage storage;
 
    // 構造函數,設置倉庫
    public Producer(Storage storage) {
        this.storage = storage;
    }
 
    // 線程run函數
    public void run() {
        produce(num);
    }
 
    // 調用倉庫Storage的生產函數
    public void produce(int num) {
        storage.produce(num);
    }
 
    // get/set方法
    public int getNum() {
        return num;
    }
 
    public void setNum(int num) {
        this.num = num;
    }
 
    public Storage getStorage() {
        return storage;
    }
 
    public void setStorage(Storage storage) {
        this.storage = storage;
    }
}
 
 
 
public class Storage {
    // 倉庫最大存儲量
    private final int MAX_SIZE = 100;
 
    // 倉庫存儲的載體
    private LinkedList<Object> list = new LinkedList<Object>();
 
    // 生產num個產品
    public void produce(int num) {
        // 同步代碼段
        synchronized (list) {
            // 若是倉庫剩餘容量不足
            while (list.size() + num > MAX_SIZE) {
                System.out.println("【要生產的產品數量】:" + num + "/t【庫存量】:"
                        + list.size() + "/t暫時不能執行生產任務!");
                try {
                    // 因爲條件不知足,生產阻塞
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
 
            // 生產條件知足狀況下,生產num個產品
            for (int i = 1; i <= num; ++i) {
                list.add(new Object());
            }
 
            System.out.println("【已經生產產品數】:" + num + "/t【現倉儲量爲】:" + list.size());
 
            list.notifyAll();
        }
    }
 
    // 消費num個產品
    public void consume(int num) {
        // 同步代碼段
        synchronized (list) {
            // 若是倉庫存儲量不足
            while (list.size() < num) {
                System.out.println("【要消費的產品數量】:" + num + "/t【庫存量】:"
                        + list.size() + "/t暫時不能執行生產任務!");
                try {
                    // 因爲條件不知足,消費阻塞
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
 
            // 消費條件知足狀況下,消費num個產品
            for (int i = 1; i <= num; ++i) {
                list.remove();
            }
 
            System.out.println("【已經消費產品數】:" + num + "/t【現倉儲量爲】:" + list.size());
 
            list.notifyAll();
        }
    }
 
    // get/set方法
    public LinkedList<Object> getList() {
        return list;
    }
 
    public void setList(LinkedList<Object> list) {
        this.list = list;
    }
 
    public int getMAX_SIZE() {
        return MAX_SIZE;
    }
}
 
public class Test {
    public static void main(String[] args)  
    {  
        // 倉庫對象  
        Storage storage = new Storage();  
   
        // 生產者對象  
        Producer p1 = new Producer(storage);  
        Producer p2 = new Producer(storage);  
        Producer p3 = new Producer(storage);  
        Producer p4 = new Producer(storage);  
        Producer p5 = new Producer(storage);  
        Producer p6 = new Producer(storage);  
        Producer p7 = new Producer(storage);  
   
        // 消費者對象  
        Consumer c1 = new Consumer(storage);  
        Consumer c2 = new Consumer(storage);  
        Consumer c3 = new Consumer(storage);  
   
        // 設置生產者產品生產數量  
        p1.setNum(10);  
        p2.setNum(10);  
        p3.setNum(10);  
        p4.setNum(10);  
        p5.setNum(10);  
        p6.setNum(10);  
        p7.setNum(80);  
   
        // 設置消費者產品消費數量  
        c1.setNum(50);  
        c2.setNum(20);  
        c3.setNum(30);  
   
        // 線程開始執行  
        c1.start();  
        c2.start();  
        c3.start();  
        p1.start();  
        p2.start();  
        p3.start();  
        p4.start();  
        p5.start();  
        p6.start();  
        p7.start();  
    }
}

應用案例   http://blog.chinaunix.net/uid-20680669-id-3602844.htmlhtml

相關文章
相關標籤/搜索