java.util.concurrent.LinkedBlockingQueue

一、简介

    LinkedBlockingQueue是BlockingQueue的一种使用Link List的实现,它对头和尾(取和添加操作)采用两把不同的锁,相对于ArrayBlockingQueue提高了吞吐量。它也是一种阻塞型的容器,适合于实现“消费者生产者”模式。

二、具体实现

    LinkedBlockingQueue底层的定义如下:

[java] view plain copy
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E>     
  2.         implements BlockingQueue<E>, java.io.Serializable {     
  3.     
  4.     static class Node<E> {     
  5.         /** The item, volatile to ensure barrier separating write and read */    
  6.         volatile E item;     
  7.         Node<E> next;     
  8.         Node(E x) { item = x; }     
  9.     }     
  10.     
  11.     // 支持原子操作     
  12.      private final AtomicInteger count = new AtomicInteger(0);     
  13.     
  14.     // 链表的头和尾     
  15.      private transient Node<E> head;     
  16.     private transient Node<E> last;     
  17.     
  18.     // 针对取和添加操作的两把锁及其上的条件     
  19.     private final ReentrantLock takeLock = new ReentrantLock();     
  20.     private final Condition notEmpty = takeLock.newCondition();     
  21.     private final ReentrantLock putLock = new ReentrantLock();     
  22.     private final Condition notFull = putLock.newCondition();     
  23.     
  24.    ...     
  25. }    

LinkedBlockingQueue的添加操作:


 

[java] view plain copy
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E>     
  2.         implements BlockingQueue<E>, java.io.Serializable {     
  3.     
  4.     private void insert(E x) {     
  5.         last = last.next = new Node<E>(x);     
  6.     }     
  7.     
  8.     /**   
  9.      * signal方法在被调用时,当前线程必须拥有该condition相关的锁!   
  10.      * Signal a waiting take. Called only from put/offer (which do not   
  11.      * otherwise ordinarily lock takeLock.)   
  12.      */    
  13.     private void signalNotEmpty() {     
  14.         final ReentrantLock takeLock = this.takeLock;     
  15.         takeLock.lock();     
  16.         try {     
  17.             notEmpty.signal();     
  18.         } finally {     
  19.             takeLock.unlock();     
  20.         }     
  21.     }     
  22.     
  23.     public void put(E o) throws InterruptedException {     
  24.         if (o == nullthrow new NullPointerException();     
  25.         int c = -1;     
  26.         final ReentrantLock putLock = this.putLock;     
  27.         final AtomicInteger count = this.count;     
  28.         // 使用putLock     
  29.         putLock.lockInterruptibly();     
  30.         try {     
  31.             try {     
  32.                   // 当容量已满时,等待notFull条件     
  33.             while (count.get() == capacity)     
  34.                     notFull.await();     
  35.             } catch (InterruptedException ie) {     
  36.                 notFull.signal(); // propagate to a non-interrupted thread     
  37.                 throw ie;     
  38.             }     
  39.             insert(o);     
  40.             // 取出当前值,并将原数据增加1     
  41.             c = count.getAndIncrement();     
  42.             // 容量不满,再次激活notFull上等待的put线程     
  43.         if (c + 1 < capacity)     
  44.                 notFull.signal();     
  45.         } finally {     
  46.             putLock.unlock();     
  47.         }     
  48.         // 必须先释放putLock再在notEmpty上signal,否则会造成死锁     
  49.      if (c == 0)     
  50.             signalNotEmpty();     
  51.     }     
  52.     
  53.   ...     
  54. }    


LinkedBlockingQueue的取操作:

[java] view plain copy
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E>     
  2.         implements BlockingQueue<E>, java.io.Serializable {     
  3.     
  4.     private E extract() {     
  5.         Node<E> first = head.next;     
  6.         head = first;     
  7.         E x = first.item;     
  8.         first.item = null;     
  9.         return x;     
  10.     }     
  11.     
  12.     private void signalNotFull() {     
  13.         final ReentrantLock putLock = this.putLock;     
  14.         putLock.lock();     
  15.         try {     
  16.             notFull.signal();     
  17.         } finally {     
  18.             putLock.unlock();     
  19.         }     
  20.     }     
  21.     
  22.     public E take() throws InterruptedException {     
  23.         E x;     
  24.         int c = -1;     
  25.         final AtomicInteger count = this.count;     
  26.         final ReentrantLock takeLock = this.takeLock;     
  27.         // 使用takeLock     
  28.         takeLock.lockInterruptibly();     
  29.         try {     
  30.             try {     
  31.                   // 若容量为空,等待notEmpty     
  32.                 while (count.get() == 0)     
  33.                     notEmpty.await();     
  34.             } catch (InterruptedException ie) {     
  35.                 notEmpty.signal(); // propagate to a non-interrupted thread     
  36.                 throw ie;     
  37.             }     
  38.     
  39.             x = extract();     
  40.             c = count.getAndDecrement();     
  41.             // 再次激活notEmpty     
  42.             if (c > 1)     
  43.                 notEmpty.signal();     
  44.         } finally {     
  45.             takeLock.unlock();     
  46.         }     
  47.         // take执行之前容量已满,则激活notFull     
  48.         if (c == capacity)     
  49.             signalNotFull();     
  50.         return x;     
  51.     }     
  52.     
  53.   ...     
  54. }    


 

    ConcurrentLinkedQueue是一个无锁的queue实现,它采用了一种无锁算法(在API中有说明),相比于传统的同步的queue来说吞吐量可以大大提高,同时它也不同于BlockingQueue,并不单单提供阻塞操作。它主要的目的是通过采用无锁的算法,使得read/write操作均不需要对容器加锁,提高容器吞吐量

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值