LinkedBlockingDeque源码
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                LinkedBlockingDeque源码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                介紹
一個由鏈表結(jié)構(gòu)組成的雙向阻塞隊列。隊列頭部和尾部都可以添加和移除元素,多線程并發(fā)時,可以將鎖的競爭最多降到一半。
相比于其他阻塞隊列,LinkedBlockingDeque多了addFirst、addLast、peekFirst、peekLast等方法,以first結(jié)尾的方法,表示插入、獲取獲移除雙端隊列的第一個元素。以last結(jié)尾的方法,表示插入、獲取獲移除雙端隊列的最后一個元素。
LinkedBlockingDeque是可選容量的,在初始化時可以設(shè)置容量防止其過度膨脹,如果不設(shè)置,默認容量大小為Integer.MAX_VALUE。
數(shù)據(jù)結(jié)構(gòu)
鏈表節(jié)點
static final class Node<E> {/*** null,表示節(jié)點被移除.*/E item;/*** One of:* - 真實后置節(jié)點* - 本節(jié)點,表示前置節(jié)點是tail* - null,沒有前置節(jié)點*/Node<E> prev;/*** One of:* - 真實后置節(jié)點。* - 本節(jié)點,表示后置節(jié)點是head。* - null, 沒有后置*/Node<E> next;Node(E x) {item = x;}}
 屬性
 
 /*** Pointer to first node.* 恒定式: (first == null && last == null) ||*            (first.prev == null && first.item != null)*/transient Node<E> first;/*** Pointer to last node.* 恒等式: (first == null && last == null) ||*            (last.next == null && last.item != null)*/transient Node<E> last;/** Number of items in the deque */private transient int count;/** Maximum number of items in the deque */private final int capacity;/** Main lock guarding all access */final ReentrantLock lock = new ReentrantLock();/** Condition for waiting takes */private final Condition notEmpty = lock.newCondition();/** Condition for waiting puts */private final Condition notFull = lock.newCondition(); 
 方法實現(xiàn)
 
offer,poll,peek
Queue實現(xiàn)
public boolean add(E e) {addLast(e);return true;}public E remove() {return removeFirst();}public E element() {return getFirst();}public boolean offer(E e) {return offerLast(e);}public boolean offer(E e, long timeout, TimeUnit unit)throws InterruptedException {return offerLast(e, timeout, unit);} public E poll() {return pollFirst();}public E poll(long timeout, TimeUnit unit) throws InterruptedException {return pollFirst(timeout, unit);}public E peek() {return peekFirst();}Queue接口的實現(xiàn)都是調(diào)用Deque接口的實現(xiàn)。
入隊列默認在Last方向(即尾端)處理。出隊列默認在First方向(即Head)處理。
Deque實現(xiàn)
public boolean offerFirst(E e) {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);final ReentrantLock lock = this.lock;lock.lock();try {return linkFirst(node);} finally {lock.unlock();}}public boolean offerLast(E e) {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);final ReentrantLock lock = this.lock;lock.lock();try {return linkLast(node);} finally {lock.unlock();}}public E pollFirst() {final ReentrantLock lock = this.lock;lock.lock();try {return unlinkFirst();} finally {lock.unlock();}}public E pollLast() {final ReentrantLock lock = this.lock;lock.lock();try {return unlinkLast();} finally {lock.unlock();}}public boolean offerFirst(E e, long timeout, TimeUnit unit)throws InterruptedException {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);long nanos = unit.toNanos(timeout);final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {while (!linkFirst(node)) {if (nanos <= 0)return false;nanos = notFull.awaitNanos(nanos);}return true;} finally {lock.unlock();}}public boolean offerLast(E e, long timeout, TimeUnit unit)throws InterruptedException {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);long nanos = unit.toNanos(timeout);final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {while (!linkLast(node)) {if (nanos <= 0)return false;nanos = notFull.awaitNanos(nanos);}return true;} finally {lock.unlock();}}public E pollFirst(long timeout, TimeUnit unit)throws InterruptedException {long nanos = unit.toNanos(timeout);final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {E x;while ( (x = unlinkFirst()) == null) {if (nanos <= 0)return null;nanos = notEmpty.awaitNanos(nanos);}return x;} finally {lock.unlock();}}public E pollLast(long timeout, TimeUnit unit)throws InterruptedException {long nanos = unit.toNanos(timeout);final ReentrantLock lock = this.lock;lock.lockInterruptibly();try {E x;while ( (x = unlinkLast()) == null) {if (nanos <= 0)return null;nanos = notEmpty.awaitNanos(nanos);}return x;} finally {lock.unlock();}}public E peekFirst() {final ReentrantLock lock = this.lock;lock.lock();try {return (first == null) ? null : first.item;} finally {lock.unlock();}}public E peekLast() {final ReentrantLock lock = this.lock;lock.lock();try {return (last == null) ? null : last.item;} finally {lock.unlock();}}
 put,take
 
Queue實現(xiàn)
public void put(E e) throws InterruptedException {putLast(e);}public E take() throws InterruptedException {return takeFirst();}Queue接口的實現(xiàn)都是調(diào)用Deque接口的實現(xiàn)。
入隊列默認在Last方向(即尾端)處理。出隊列默認在First方向(即Head)處理。
?Deque實現(xiàn)
public void putFirst(E e) throws InterruptedException {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);final ReentrantLock lock = this.lock;lock.lock();try {while (!linkFirst(node))notFull.await();} finally {lock.unlock();}}public void putLast(E e) throws InterruptedException {if (e == null) throw new NullPointerException();Node<E> node = new Node<E>(e);final ReentrantLock lock = this.lock;lock.lock();try {while (!linkLast(node))notFull.await();} finally {lock.unlock();}}public E takeFirst() throws InterruptedException {final ReentrantLock lock = this.lock;lock.lock();try {E x;while ( (x = unlinkFirst()) == null)notEmpty.await();return x;} finally {lock.unlock();}}public E takeLast() throws InterruptedException {final ReentrantLock lock = this.lock;lock.lock();try {E x;while ( (x = unlinkLast()) == null)notEmpty.await();return x;} finally {lock.unlock();}}remove
public boolean removeFirstOccurrence(Object o) {if (o == null) return false;final ReentrantLock lock = this.lock;lock.lock();try {for (Node<E> p = first; p != null; p = p.next) {if (o.equals(p.item)) {unlink(p);return true;}}return false;} finally {lock.unlock();}}public boolean removeLastOccurrence(Object o) {if (o == null) return false;final ReentrantLock lock = this.lock;lock.lock();try {for (Node<E> p = last; p != null; p = p.prev) {if (o.equals(p.item)) {unlink(p);return true;}}return false;} finally {lock.unlock();}}pop,push
public void push(E e) {addFirst(e);}/*** @throws NoSuchElementException {@inheritDoc}*/public E pop() {return removeFirst();}unlinkFirst,unlinkLast,linkFirst,linkLast
private E unlinkFirst() {// assert lock.isHeldByCurrentThread();Node<E> f = first;if (f == null)return null;Node<E> n = f.next;E item = f.item;f.item = null; //表示節(jié)點移除。f.next = f; // help GCfirst = n;//沒有節(jié)點if (n == null)last = null;elsen.prev = null;--count;notFull.signal();return item;}/*** Removes and returns last element, or null if empty.*/private E unlinkLast() {// assert lock.isHeldByCurrentThread();Node<E> l = last;if (l == null)return null;Node<E> p = l.prev;E item = l.item;l.item = null;l.prev = l; // help GClast = p;if (p == null)first = null;elsep.next = null;--count;notFull.signal();return item;}private boolean linkFirst(Node<E> node) {// assert lock.isHeldByCurrentThread();if (count >= capacity)return false;//節(jié)點加入到head。Node<E> f = first;node.next = f;first = node;if (last == null)last = node;elsef.prev = node;++count;//notEmpty.signal();return true;}/*** Links node as last element, or returns false if full.*/private boolean linkLast(Node<E> node) {// assert lock.isHeldByCurrentThread();if (count >= capacity)return false;Node<E> l = last;node.prev = l;last = node;if (first == null)first = node;elsel.next = node;++count;notEmpty.signal();return true;}void unlink(Node<E> x) {// assert lock.isHeldByCurrentThread();Node<E> p = x.prev;Node<E> n = x.next;if (p == null) {unlinkFirst();} else if (n == null) {unlinkLast();} else {p.next = n;n.prev = p;x.item = null;// Don't mess with x's links. They may still be in use by// an iterator.--count;notFull.signal();}}
 參考
 
- LinkedBlockingQueue源碼
總結(jié)
以上是生活随笔為你收集整理的LinkedBlockingDeque源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: DelayQueue源码
- 下一篇: JDK1.8并发包中的类
