java.util.concurrent.LinkedBlockingQueue

一、简介

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

二、具体实现

    LinkedBlockingQueue底层的定义如下:

public class LinkedBlockingQueue<E> extends AbstractQueue<E>   
        implements BlockingQueue<E>, java.io.Serializable {   
  
    static class Node<E> {   
        /** The item, volatile to ensure barrier separating write and read */  
        volatile E item;   
        Node<E> next;   
        Node(E x) { item = x; }   
    }   
  
    // 支持原子操作   
     private final AtomicInteger count = new AtomicInteger(0);   
  
    // 链表的头和尾   
     private transient Node<E> head;   
    private transient Node<E> last;   
  
    // 针对取和添加操作的两把锁及其上的条件   
    private final ReentrantLock takeLock = new ReentrantLock();   
    private final Condition notEmpty = takeLock.newCondition();   
    private final ReentrantLock putLock = new ReentrantLock();   
    private final Condition notFull = putLock.newCondition();   
  
   ...   
}  

LinkedBlockingQueue的添加操作:


 

public class LinkedBlockingQueue<E> extends AbstractQueue<E>   
        implements BlockingQueue<E>, java.io.Serializable {   
  
    private void insert(E x) {   
        last = last.next = new Node<E>(x);   
    }   
  
    /**  
     * signal方法在被调用时,当前线程必须拥有该condition相关的锁!  
     * Signal a waiting take. Called only from put/offer (which do not  
     * otherwise ordinarily lock takeLock.)  
     */  
    private void signalNotEmpty() {   
        final ReentrantLock takeLock = this.takeLock;   
        takeLock.lock();   
        try {   
            notEmpty.signal();   
        } finally {   
            takeLock.unlock();   
        }   
    }   
  
    public void put(E o) throws InterruptedException {   
        if (o == null) throw new NullPointerException();   
        int c = -1;   
        final ReentrantLock putLock = this.putLock;   
        final AtomicInteger count = this.count;   
        // 使用putLock   
        putLock.lockInterruptibly();   
        try {   
            try {   
                  // 当容量已满时,等待notFull条件   
            while (count.get() == capacity)   
                    notFull.await();   
            } catch (InterruptedException ie) {   
                notFull.signal(); // propagate to a non-interrupted thread   
                throw ie;   
            }   
            insert(o);   
            // 取出当前值,并将原数据增加1   
            c = count.getAndIncrement();   
            // 容量不满,再次激活notFull上等待的put线程   
        if (c + 1 < capacity)   
                notFull.signal();   
        } finally {   
            putLock.unlock();   
        }   
        // 必须先释放putLock再在notEmpty上signal,否则会造成死锁   
     if (c == 0)   
            signalNotEmpty();   
    }   
  
  ...   
}  


LinkedBlockingQueue的取操作:

public class LinkedBlockingQueue<E> extends AbstractQueue<E>   
        implements BlockingQueue<E>, java.io.Serializable {   
  
    private E extract() {   
        Node<E> first = head.next;   
        head = first;   
        E x = first.item;   
        first.item = null;   
        return x;   
    }   
  
    private void signalNotFull() {   
        final ReentrantLock putLock = this.putLock;   
        putLock.lock();   
        try {   
            notFull.signal();   
        } finally {   
            putLock.unlock();   
        }   
    }   
  
    public E take() throws InterruptedException {   
        E x;   
        int c = -1;   
        final AtomicInteger count = this.count;   
        final ReentrantLock takeLock = this.takeLock;   
        // 使用takeLock   
        takeLock.lockInterruptibly();   
        try {   
            try {   
                  // 若容量为空,等待notEmpty   
                while (count.get() == 0)   
                    notEmpty.await();   
            } catch (InterruptedException ie) {   
                notEmpty.signal(); // propagate to a non-interrupted thread   
                throw ie;   
            }   
  
            x = extract();   
            c = count.getAndDecrement();   
            // 再次激活notEmpty   
            if (c > 1)   
                notEmpty.signal();   
        } finally {   
            takeLock.unlock();   
        }   
        // take执行之前容量已满,则激活notFull   
        if (c == capacity)   
            signalNotFull();   
        return x;   
    }   
  
  ...   
}  


 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值