java常见面试考点(四十六):生产者消费者

java常见面试考点

往期文章推荐:
  java常见面试考点(四十一):short运算小错误
  java常见面试考点(四十二):序列化与反序列化
  java常见面试考点(四十三):泛型
  java常见面试考点(四十四):Comparable和Comparator接口
  java常见面试考点(四十五):阻塞队列


【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权);

本博客的内容来自于:java常见面试考点(四十六):生产者消费者

学习、合作与交流联系q384660495;

本博客的内容仅供学习与参考,并非营利;


一、Java能实现的几种方法

  • wait() / notify()方法

  • await() / signal()方法

  • BlockingQueue阻塞队列方法

  • 信号量

  • 管道

二、wait() / notify()方法

class ShareData{
    private int number = 0;

    public void increment() throws InterruptedException {
        synchronized (this){
            while(number != 0){
                this.wait();
            }
            number++;
            System.out.println("increment number = " + number);
            this.notifyAll();
        }
    }

    public void decrement() throws InterruptedException {
        synchronized (this){
            while(number == 0){
                this.wait();
            }
            number--;
            System.out.println("decrement number = " + number);
            this.notifyAll();
        }
    }
}

三、await() / signal()方法

class ShareData{
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while (number!=0){
                System.out.println("ShareData.increment");
                condition.await();
            }
            number++;
            System.out.println("increment number = " + number);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (number==0){
                System.out.println("ShareData.decrement");
                condition.await();
            }
            number--;
            System.out.println("decrement number = " + number);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}

四、BlockingQueue阻塞队列方法

class MyResource{
    private volatile boolean FLAG = true;
    private AtomicInteger integer = new AtomicInteger();

    BlockingQueue<String> blockingQueue = null;

    public MyResource(BlockingQueue<String> blockingQueue){
        this.blockingQueue = blockingQueue;
        System.out.println("blockingQueue.getClass().getName() = " + blockingQueue.getClass().getName());
    }

    public void product() throws InterruptedException {
        String data = null;
        boolean flag;
        while (FLAG){
            data = integer.incrementAndGet() + "";
            flag = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if(flag){
                System.out.println("生产线程启动: " + flag);
            }else {
                System.out.println("生产线程启动失败 " );
            }
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println("product  over ");
    }

    public void consume() throws InterruptedException {
        String data = null;
        boolean flag;
        while (FLAG){
            data = integer.incrementAndGet() + "";
            String poll = blockingQueue.poll(2L, TimeUnit.SECONDS);
            System.out.println("消费线程取出: " + poll);
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println("consume  over ");
    }

    public void stop(){
        this.FLAG = false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天的爱人是绿色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值