JAVA并发编程工具篇--3理解BlockingQueue

前言:在生产者和消费者模型实现中,使用生产者和消费者线程对消息队列的生产和消费,并用wait() 和notify() 来协调线程进行通信;那么有没有一种队列可以自身实现这个功能,在队列满了后阻塞消费者唤醒生产者,而在队列为空的时候阻塞消费者唤醒生产者,在java 中提供BlockingQueue 阻塞队列来对改场景进行实现

1 使用:
通过 ArrayBlockingQueue或者 LinkedBlockingQueue 声明阻塞队列;
1.1 使用ArrayBlockingQueue 实现:

// 声明阻塞队列
BlockingQueue blockingQueue = new ArrayBlockingQueue(5);
// blockingQueue.add("123");
// blockingQueue.offer("456");
new Thread(() -> {
    for (; ; ) {
        try {
            String msg = UUID.randomUUID().toString();
            blockingQueue.put(msg);// 生产者生产消息
            System.out.println("生产消息" + msg);
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();
Thread.sleep(2000);

new Thread(() -> {
    for (; ; ) {
        try {
            String msg = blockingQueue.take().toString();
            System.out.println("消费消息" + msg);// 消费者消费消息
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();

1.2 使用LinkedBlockingQueue实现:

// 声明阻塞队列
BlockingQueue blockingQueue2 = new LinkedBlockingQueue(10);
// blockingQueue2.add("123");
// blockingQueue2.offer("456");
new Thread(() -> {
    for (; ; ) {
        try {
            String msg = UUID.randomUUID().toString();
            blockingQueue2.put(msg);// 生产消息
            System.out.println("生产消息" + msg);
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();
Thread.sleep(2000);

new Thread(() -> {
    for (; ; ) {
        try {
            String msg = blockingQueue2.take().toString();// 消费消息
            System.out.println("消费消息" + msg);
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();

2 过程:
2.1 ArrayBlockingQueue:
2.1.1 声明阻塞队列:

// 声明阻塞队列
BlockingQueue blockingQueue = new ArrayBlockingQueue(5);
// ArrayBlockingQueue  构造方法,传入队列的长度
public ArrayBlockingQueue(int capacity) {
    this(capacity, false);
}
public ArrayBlockingQueue(int capacity, boolean fair) {
    if (capacity <= 0)// 队列长度为0 抛出异常
        throw new IllegalArgumentException();
    // 声明数组长度
    this.items = new Object[capacity];
    // 声明锁
    lock = new ReentrantLock(fair);
    // 声明队列不为空的条件
    notEmpty = lock.newCondition();
    // 声明队列不满的条件
    notFull =  lock.newCondition();
}
// 属性
/** The queued items */
 final Object[] items;

 /** items index for next take, poll, peek or remove */
 int takeIndex;

 /** items index for next put, offer, or add */
 int putIndex;

 /** Number of elements in the queue */
 int count;

 /*
  * Concurrency control uses the classic two-condition algorithm
  * found in any textbook.
  */

 /** Main lock guarding all access */
 final ReentrantLock lock;

 /** Condition for waiting takes */
 private final Condition notEmpty;

 /** Condition for waiting puts */
 private final Condition notFull;

 /**
  * Shared state for currently active iterators, or null if there
  * are known not to be any.  Allows queue operations to update
  * iterator state.
  */
 transient Itrs itrs = null;

2.1.2 添加元素:
offer(E e):

public boolean offer(E e) {
    checkNotNull(e);// 元素为空抛出异常
    final ReentrantLock lock = this.lock;// 添加元素先要获取锁
    lock.lock();
    try {
        if (count == items.length)// count =0 ; 这里判断队列已满则直接返回false
            return false;
        else {
            enqueue(e);// 向队列中添加元素
            return true;
        }
    } finally {
        lock.unlock();// 释放锁
    }
}
private static void checkNotNull(Object v) {
    if (v == null)
        throw new NullPointerException();
}
private void enqueue(E x) {
    // assert lock.getHoldCount() == 1;
    // assert items[putIndex] == null;
    final Object[] items = this.items;// 获取数组数据
    items[putIndex] = x;// 存入元素到数组下标的位置,putIndex 初始为0
    if (++putIndex == items.length)// 存入后,计算下一次存入元素的位置
        putIndex = 0;//  如果已达到数组的末端,则下次当队列不满的时候存入数组开端的位置
    count++;// 数组中存放数据的计数量加1
    notEmpty.signal();// 唤醒消费者消费消息
}

add(E e):

public boolean add(E e) {
    return super.add(e);
}
public boolean add(E e) {
    if (offer(e))// 直接调用offer 方法添加元素
        return true;
    else
        throw new IllegalStateException("Queue full");//如果添加失败则直接抛出异常
}

put(E e):

public void put(E e) throws InterruptedException {
    checkNotNull(e);// 添加的元素不能为null
    final ReentrantLock lock = this.lock;// 获取锁
    lock.lockInterruptibly();
    try {
        while (count == items.length)// 当队列的长度已满
            notFull.await();// 阻塞生产者
        enqueue(e);// 队列不满,添加元素到队列中
    } finally {
        lock.unlock();// 释放锁
    }
}

2.1.3 获取元素:
poll():

public E poll() {
    final ReentrantLock lock = this.lock;// 获取锁
    lock.lock();
    try {
        return (count == 0) ? null : dequeue();// 如果队列为空则直接返回null;否则进入 dequeue()
    } finally {
        lock.unlock();
    }
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
    // assert items[takeIndex] != null;
    final Object[] items = this.items;// 获取队列数据
    @SuppressWarnings("unchecked")
    E x = (E) items[takeIndex];// 从takeIndex 下标出获取元素,takeIndex 初始为0
    items[takeIndex] = null;// 将takeIndex元素数据置空
    if (++takeIndex == items.length)// 计算下次获取元素的下标位置
        takeIndex = 0;// 如果已达到数组末端则从数组开端继续获取元素
    count--;// 队列的实际长度-1
    if (itrs != null)// itrs  不为null  ,初始itrs  为null
        itrs.elementDequeued();
    notFull.signal();// 唤醒生产者生产消息
    return x;// 返回本次获取到的数据
}

remove():
调用 AbstractQueue.remove():

public E remove() {
    E x = poll();// 通过poll() 获取元素
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

take():

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;// 获取锁
    lock.lockInterruptibly();
    try {
        while (count == 0)// 如果队列为空
            notEmpty.await();// 阻塞消费者
        return dequeue();// 从队列中获取数据
    } finally {
        lock.unlock();// 释放锁
    }
}

2.2 LinkedBlockingQueue:
2.2.1 声明阻塞队列:

BlockingQueue blockingQueue2 = new LinkedBlockingQueue(10);
public LinkedBlockingQueue(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();// 队列长度为0 抛异常
    this.capacity = capacity;// 队列长度赋值
    last = head = new Node<E>(null);// 头结点和为节点 初始化
}

2.2.2 放入元素:
offer():

 public boolean offer(E e) {
  if (e == null) throw new NullPointerException();
    final AtomicInteger count = this.count;// 队列的实际长度
    if (count.get() == capacity) //如果队列已满则直接返回false
        return false;
    int c = -1;
    Node<E> node = new Node<E>(e);// 声明一个节点元素
    final ReentrantLock putLock = this.putLock;// 获取锁
    putLock.lock();
    try {
        if (count.get() < capacity) {// 如果队列不满
            enqueue(node);// 放入元素
            c = count.getAndIncrement();// c+1
            if (c + 1 < capacity)// 如果队列不满
                notFull.signal();// 唤醒生产者
        }
    } finally {
        putLock.unlock();// 是否锁
    }
    if (c == 0)// 第一次为队列添加元素后
        signalNotEmpty();// 唤醒消费者消费信息
    return c >= 0;
}
private void enqueue(Node<E> node) {
     // assert putLock.isHeldByCurrentThread();
     // assert last.next == null;
     last = last.next = node;// node 节点插入到单向链表的尾部
 }
private void signalNotEmpty() {
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        notEmpty.signal();// 唤醒消费者
    } finally {
        takeLock.unlock();
    }
}

add(E e):

public boolean add(E e) {
  if (offer(e))// 调用offer 方法
        return true;
    else
        throw new IllegalStateException("Queue full");
}

put(E e):

public void put(E e) throws InterruptedException {
  if (e == null) throw new NullPointerException();// 放入的元素为null 则抛出异常
    // Note: convention in all put/take/etc is to preset local var
    // holding count negative to indicate failure unless set.
    int c = -1;// c 标识
    Node<E> node = new Node<E>(e);// 构建node 节点
    final ReentrantLock putLock = this.putLock;// 获取锁
    final AtomicInteger count = this.count;// 获取队列的实际长度
    putLock.lockInterruptibly();
    try {
        /*
         * Note that count is used in wait guard even though it is
         * not protected by lock. This works because count can
         * only decrease at this point (all other puts are shut
         * out by lock), and we (or some other waiting put) are
         * signalled if it ever changes from capacity. Similarly
         * for all other uses of count in other wait guards.
         */
        while (count.get() == capacity) {// 如果队列已满
            notFull.await();// 阻塞生产者
        }
        enqueue(node);// 将node 节点放入到单向链表的尾部
        c = count.getAndIncrement();// 将count 队列长度加1,并返回之前队列的长度
        if (c + 1 < capacity)
            notFull.signal();// 如果队列不满,唤醒生产者
    } finally {
        putLock.unlock();// 释放锁
    }
    if (c == 0)// 如果队列第一次被放入元素
        signalNotEmpty();// 唤醒消费者消费消息
}

2.2.3 获取元素:
poll():

 public E poll() {
   final AtomicInteger count = this.count;// 当前队列的实际长度
    if (count.get() == 0)// 队列没有元素直接返回null
        return null;
    E x = null;// 要获取的元素
    int c = -1;// 标识
    final ReentrantLock takeLock = this.takeLock;// 获取锁
    takeLock.lock();
    try {
        if (count.get() > 0) {// 队列有元素
            x = dequeue();// 获取队列单向链表首个节点
            c = count.getAndDecrement();// 将队列实际长度减1,并返回之前的
            if (c > 1)// 之前的实际数量大于1
                notEmpty.signal();// 唤醒消费者继续消费信息
        }
    } finally {
        takeLock.unlock();// 释放锁
    }
    if (c == capacity)//  之前的实际数量已经达到最大值 
        signalNotFull();
    return x;
}
private void signalNotFull() {
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        notFull.signal();// 唤醒生产者生产消息
    } finally {
        putLock.unlock();
    }
}

remove():
调用 AbstractQueue.remove():

ublic E remove() {
   E x = poll();// 通过poll 方法获取队列元素
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

take():

public E take() throws InterruptedException {
   	E x;// 
    int c = -1;
    final AtomicInteger count = this.count;// 获取锁
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {// 当队列的实际长度为0 
            notEmpty.await();// 阻塞消费者
        }
        x = dequeue();// 获取队列链表的首节点
        c = count.getAndDecrement();// count 减1,并返回链表之前长度
        if (c > 1)// 如果长度大于1
            notEmpty.signal();// 唤醒消费者消费消息
    } finally {
        takeLock.unlock();// 释放锁
    }
    if (c == capacity)// 如果链表之前的长度为最大长度,此时获取元素后队列不满
        signalNotFull();//  唤醒生产者生产消息
    return x;
}
private E dequeue() {
    // assert takeLock.isHeldByCurrentThread();
    // assert head.item == null;
    Node<E> h = head;// 获取链表头节点
    Node<E> first = h.next;// 链表首节点
    h.next = h; // help GC// 将原有首节点置null
    head = first;// 重置链表首节点
    E x = first.item;// 获取元素
    first.item = null;// 链表首节点值置null
    return x;
}

3 总结:
BlockingQueue 阻塞队列,通过定义队列的长度,当队列长度满时,通过await 阻塞生产者线程,通过notify 唤醒消费者线程;当队列长度为0时,通过await 阻塞消费者线程,通过notify 唤醒生产者线程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值