溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-09 12:40:25 來(lái)源:網(wǎng)絡(luò) 閱讀:481 作者:shayang88 欄目:編程語(yǔ)言

案例介紹

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

代碼說(shuō)明

生產(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;

    }
    //同步放入一個(gè)蛋糕
    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();

    }
    //同步獲取一個(gè)蛋糕
    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;
    }

}

啟動(dòng)文件

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é)果

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI