经典面试题: 手写生产者 /消费者模型

通常情况下,有5种方式来实现

  1. synchronized + wait() + notify() 方式
  2. 可重入锁ReentrantLock (配合Condition)
  3. BlockingQueue 阻塞队列方式
  4. 信号量Semaphore 方式
  5. 管道输入输出流PipedInputStream和PipedOutputStream 方式

下面代码是 采用 第二种方式

package interview.producer_consumer;

import lombok.SneakyThrows;

import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    public static void main(String[] args) {

        MyDeque myDeque = new MyDeque(10);
        Producer_ producer_ = new Producer_(myDeque);
        Consumer_ consumer_ = new Consumer_(myDeque);
        Producer_ producer_2 = new Producer_(myDeque);
        Consumer_ consumer_2 = new Consumer_(myDeque);

        new Thread( producer_).start();
        new Thread( consumer_).start();

        new Thread( producer_2).start();
        new Thread( consumer_2).start();
    }
}

class MyDeque {
    int max_size;
    LinkedList<Integer> linkedList = new LinkedList<Integer>();
    ReentrantLock reentrantLock = new ReentrantLock();
    Condition producer_condition = reentrantLock.newCondition();//生产者条件:消息队列满了阻塞
    Condition consumer_condition = reentrantLock.newCondition();//消费者条件:消息队列为空阻塞

    volatile int data = 1;



    public MyDeque(int max_size){
        this.max_size = max_size;
    }
    public void  put(){
        reentrantLock.lock();
        try {
            if (linkedList.size() >= this.max_size){
                System.out.println(" 队列已满" + Thread.currentThread().getName() + "无法添加数据");
                producer_condition.await();
            }
            System.out.print( Thread.currentThread().getName() +"生产者添加数据" + data + "\t");
            linkedList.add( this.data);
            data++;

            System.out.println("队列数据" + linkedList);
            consumer_condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }
    public void take(){
        reentrantLock.lock();
        try {
            if (linkedList.size() >0 ){
                Integer integer = linkedList.removeFirst();
                System.out.println(Thread.currentThread().getName() +  "消费者 消费数据" + integer);
            }else {
                System.out.println("队列中无数据, 消费者等待");
                producer_condition.signalAll();
                consumer_condition.await();
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }
}

class Producer_ implements Runnable{
    MyDeque myDeque;
    public Producer_(MyDeque myDeque ){
        this.myDeque = myDeque;
    }

    @SneakyThrows
    @Override
    public void run() {
        while (true){
            myDeque.put();
            Thread.sleep(1000);
        }
    }
}

class Consumer_ implements Runnable{
    MyDeque myDeque;
    public   Consumer_(MyDeque myDeque ){
        this.myDeque = myDeque;
    }

    @SneakyThrows
    @Override
    public void run() {
       while (true){
           myDeque.take();
           Thread.sleep(1500);
       }

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值