阻塞队列的使用

阻塞队列使用

实现接口:

Iterator

collection

Queue

BlockingQueue

实现类:

ArrayBlocking:底层数组,有界

LinkedBlockingQueue:底层链表,无界(int最大值)

源码:

    public void put(E e) throws InterruptedException {
        Objects.requireNonNull(e);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == items.length) //判断队列中的数据是否为0
                notFull.await();
            enqueue(e); //没有数据,加入
        } finally {
            lock.unlock();
        }
    }
    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0) //判读队列中的数据是否为0
                notEmpty.await();
            return dequeue(); //有数据获取
        } finally {
            lock.unlock();
        }
    }

生产者消费者问题:

不用写锁

package day0920;

import java.util.concurrent.ArrayBlockingQueue;

public class Cooker extends Thread{
    private ArrayBlockingQueue<String> cc;
    private String name;
    public Cooker(ArrayBlockingQueue<String> cc,String name){
        this.cc = cc;
        this.name = name;
    }

    @Override
    public void run() {

            while (true){
          synchronized (Cooker.class){
              try {
                  String nme = String.valueOf(System.currentTimeMillis());
                  cc.put(nme);
                  System.out.println(name + nme+ "做了一个包子");
                  Thread.sleep(200);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
          }
            }

    }
}

package day0920;

import java.util.concurrent.ArrayBlockingQueue;

public class Eater extends Thread{
    private ArrayBlockingQueue<String> ee;
    private String name;

    public Eater(ArrayBlockingQueue<String> ee,String name){
        this.ee = ee;
        this.name = name;

    }

    @Override
    public void run() {
        while (true){
        synchronized (Eater.class){
            try {
                String take = ee.take();
                System.out.println(name + take + "吃了一个包子");
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
      }
    }
}

package day0920;

import java.util.concurrent.ArrayBlockingQueue;

public class CookAndEat {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> arr = new ArrayBlockingQueue<>(1);
        Cooker cooker = new Cooker(arr,"灰太郎");
        Cooker cooker2 = new Cooker(arr,"小福贵");
        Cooker cooker3 = new Cooker(arr,"小当家");
        Eater eater = new Eater(arr,"懒洋洋");
        Eater eater2 = new Eater(arr,"小灰灰");
            cooker3.start();
            cooker2.start();
            cooker.start();
            eater.start();
            eater2.start();


    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值