1. 程式人生 > >多線程(二、生產者-消費者)

多線程(二、生產者-消費者)

com 生產者 creat system sum 圖片 蛋糕 super throw

案例介紹

生產者:Producer,消費者Consumer,消費品,Cake,消費品存放隊列CakeQueue

代碼說明

生產者Producer

public class Producer extends Thread {
    private final CakeQueue cakeQueue;
    private static int cakeNo = 0;
    public Producer(String name, CakeQueue cakeQueue){
        super(name);
        this.cakeQueue = cakeQueue;
    }
    public void run(){
        try {
            while (true) {
                Thread.sleep(1000);
                Cake cake = new Cake(createCakeNo());
                this.cakeQueue.put(cake);
            }
        } catch (InterruptedException e) {
        }
    }
    private static synchronized int createCakeNo() {
        return cakeNo++;
    }

}

消費者Consumer

public class Consumer extends Thread{
    private final CakeQueue cakeQueue;
    public Consumer(String name, CakeQueue cakeQueue) {
        super(name);
        this.cakeQueue = cakeQueue;
    }
    public void run(){
        try {
            while (true) {
                Cake cake = cakeQueue.take();
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
        }
    }
}

蛋糕Cake

/**
 * 蛋糕
 */
public class Cake {
    private final int no;
    public Cake(int no) {
        this.no = no;
    }

    public int getNo() {
        return this.no;
    }

    public String toString(){
        return "[ Cake No." + no + "]";
    }

}

隊列CakeQueue

/**
 * 存放蛋糕的隊列
 */
public class CakeQueue {
    private final Cake[] cakeList;
    private int head;
    private int tail;
    private int counter;

    public CakeQueue(int num) {
        this.cakeList = new Cake[num];
        this.head = 0;
        this.tail = 0;
        this.counter = 0;

    }
    //同步放入一個蛋糕
    public synchronized void put(Cake cake) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "put:No" + cake.getNo());
        while(counter >= cakeList.length){
            wait();
        }
        cakeList[tail] = cake;
        tail = (tail + 1)  % cakeList.length;
        counter++;
        notifyAll();

    }
    //同步獲取一個蛋糕
    public synchronized Cake take() throws InterruptedException {
        while(counter <= 0){
            wait();
        }
        Cake cake = cakeList[head];
        head = (head + 1) % cakeList.length;
        counter--;
        notifyAll();
        System.out.println("------" + Thread.currentThread().getName() + "take:No" + cake.getNo());
        return cake;
    }

}

啟動文件

public class Main {

    public static void main(String[] args) {

        CakeQueue cakeQueue = new CakeQueue(3);
        new Producer("Producer-1:", cakeQueue).start();
        new Producer("Producer-2:", cakeQueue).start();
        new Producer("Producer-3:", cakeQueue).start();
        new Consumer("Consumer-1:", cakeQueue).start();
        new Consumer("Consumer-2:", cakeQueue).start();
        new Consumer("Consumer-3:", cakeQueue).start();

    }
}

運行結果

技術分享圖片

多線程(二、生產者-消費者)