多線程(二、生產(chǎn)者-消費(fèi)者模式)

案例介紹

生產(chǎn)者:Producer,消費(fèi)者Consumer,消費(fèi)品,Cake,消費(fèi)品存放隊(duì)列CakeQueue

代碼說明

生產(chǎn)者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++;
    }

}

消費(fèi)者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 + "]";
    }

}

隊(duì)列CakeQueue

/**
 * 存放蛋糕的隊(duì)列
 */
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();

    }
}

運(yùn)行結(jié)果

多線程(二、生產(chǎn)者-消費(fèi)者模式)

成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為玉泉街道等服務(wù)建站,玉泉街道等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為玉泉街道企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

網(wǎng)頁題目:多線程(二、生產(chǎn)者-消費(fèi)者模式)
當(dāng)前網(wǎng)址:http://www.muchs.cn/article2/jiojoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序面包屑導(dǎo)航、響應(yīng)式網(wǎng)站云服務(wù)器、品牌網(wǎng)站制作商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司