基於BlockingQueue的生產者-消費者模型

不管是在學習操作系統知識還是Java多線程知識的時候,都會遇到生產者-消費者模型。我們必須熟練地寫出一個簡單的模型。之前的使用的大多數都是synchronized鎖同步代碼塊或修飾方法或是使用ReetrantLock來完成,這裏介紹使用java.util.concurrent包提供的BlockingQueue來實現。

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class CakeShop{
    //使用volatile修飾,保證多個線程之間的可見性
    private volatile boolean FLAG = true;
    private AtomicInteger cake = new AtomicInteger(0);
    private BlockingQueue<String> blockingQueue;

    //傳參傳接口,方便多種不同的需求
    CakeShop(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

  public void production(){
        String data;
        boolean retValue = false;

        //多線程情況下,使用while進行條件判斷
        while (FLAG) {
            data = cake.incrementAndGet() + "";
            try {
                retValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (retValue) {
                System.out.println(Thread.currentThread().getName() + "\t生產隊列,生產蛋糕" + data + ",成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t生產隊列,生產蛋糕" + data + ",失敗");
            }

            //模擬生產過程
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        System.out.println((Thread.currentThread().getName() + "\tflag=false,停止生產!"));

    }

    public void consumption(){
        String res = "";
        while (FLAG) {

            try {
                res = blockingQueue.poll(2L, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (res == null || res.equalsIgnoreCase("")) {
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 超過兩秒鐘未取到蛋糕,停止消費");
                System.out.println();
                System.out.println();
                return;
            }

            System.out.println(Thread.currentThread().getName() + "\t 消費隊列,消費蛋糕" + res + ",成功!");
        }
    }

    public void stop(){
        this.FLAG = false;
    }
}



public class ProdConsumerWithBlockingQueue {

    public static void main(String[] args) {
        CakeShop cakeShop = new CakeShop(new ArrayBlockingQueue<>(10));

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t啓動");
            cakeShop.production();
        }, "生產者線程").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t啓動");

            try {
                cakeShop.consumption();
                System.out.println();
                System.out.println();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "消費者線程").start();


        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("============================");

        System.out.println("五秒鐘時間到,停止活動!");

        cakeShop.stop();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章