java 使用信号量Semaphore实现生产者-消费者模式

这是继许上一篇java 使用synchronized, wait(), notifyAll() 实现生产者-消费者模式

信号量Semaphore,跟交通信号等非常类似(Semaphore翻译过来就是信号灯的意思),以下面这幅图为例

如果两条铁轨都是空的,那么此时信号灯是绿色(信号量为2),允许火车通行。如果有列车请求通行则放行,同时信号灯变为黄色(信号量减一):

当两条铁轨都有列车通行时,信号灯为红色(信号量为0),不允许火车通过。若果有列车请通行则阻塞:

当一辆列车离开铁轨的后,信号灯变为黄色(信号量为1),此时等待的通行的列车被放行:

 

 有了上面的感性认识后,我们来看看到底如何表示信号量。

可定用一个整数表示信号量,然后有一个增加操作和一个减少操作,于是有如下的

class Semaphore{
    private volatile int permit;
    
    public Semaphore(int permit){
        this.permit = permit;
    }
    
    public acquire(){
        if (permit <= 0){
            //等待
        }
        //执行permit--操作
    }

    public release(){
        //执行permit++ 操作
        if(permit > 0){
            //唤醒
        }
    }
}
  • 这里有两点需要说明:
  1. 这里先用一个二进制信号量来等效互斥操作;
  2.  由于信号量只能通过0值来进行阻塞和唤醒,所以这里必须使用两个信号量来模拟容器空和容器满两种状态

 

public class Cache {
    private int cacheSize = 0;

    public Semaphore mutex;
    public Semaphore empty; //保证了容器空的时候(empty的信号量<=0), 消费者等待
    public Semaphore full;  //保证了容器满的时候(full的信号量 <= 0),生产者等待

    public Cache(int size) {
        mutex = new Semaphore(1);   //二进制信号量,表示互斥锁
        empty = new Semaphore(size);
        full = new Semaphore(0);
    }

    public int getCacheSize()throws InterruptedException{
        return cacheSize;
    }

    public void produce() throws InterruptedException{
        empty.acquire();    // 消耗一个空位
        mutex.acquire();
        cacheSize++;
        System.out.println("生产了一个产品, 当前产品数为" + cacheSize);
        mutex.release();
        full.release();     // 增加了一个产品


    }

    public void consume() throws InterruptedException{
        full.acquire();     // 消耗了一个产品
        mutex.acquire();
        cacheSize--;
        System.out.println("消费了一个产品, 当前产品数为" + cacheSize);
        mutex.release();
        empty.release();    // 增加了一个空位

    }
}
public class Consumer implements Runnable {
    private Cache cache;

    public Consumer(Cache cache){
        this.cache = cache;
    }

    @Override
    public void run() {
        while(true){
            try {
                cache.consume();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
public class Producer implements Runnable {
    private Cache cache;

    public Producer(Cache cache){
        this.cache = cache;
    }

    @Override
    public void run() {
        while(true){
            try {
                cache.produce();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Cache cache = new Cache(10);

        Producer p = new Producer(cache);
        Consumer c = new Consumer(cache);

        int producerCount = 4, consumerCount = 4;
        for (int i = 0; i < producerCount; i++){
            new Thread(p).start();
        }
        for (int i = 0; i < consumerCount; i++){
            new Thread(c).start();
        }
    }
}

未完待续。。。(下一篇:通过管程(Monitor)来实现生产者-消费者模式)

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值