LinkedBlockingQueue源码讲解
来源:互联网 发布:vb.net 释放资源文件 编辑:程序博客网 时间:2024/05/20 03:47
尊重原创,转载请标明出处 http://blog.csdn.net/abcdef314159
源码:\sources\Android-25
说到LinkedBlockingQueue,不得不提到LinkedBlockingDeque,他俩差不多,只不过LinkedBlockingDeque是双向的队列,而LinkedBlockingQueue是单向的队列,我们看一下他的节点就知道了
/** * Linked list node class. */ static class Node<E> { E item; /** * One of: * - the real successor Node * - this Node, meaning the successor is head.next * - null, meaning there is no successor (this is the last node) */ Node<E> next; Node(E x) { item = x; } }Node只有后一个没有前一个,并且他是有容量限制的,容量大小是capacity,容量满了如果再加入就会阻塞。来看第一个方法dequeue
/** * Removes a node from head of queue. * * @return the node */ //移除一个元素 private E dequeue() { // assert takeLock.isHeldByCurrentThread(); // assert head.item == null; Node<E> h = head; Node<E> first = h.next;//让自己的下一个指向自己,相当于与链表断开了。 h.next = h; // help GC//然后让head的先一个成为head元素。 head = first;//head元素的item是空的。 E x = first.item; first.item = null; return x; }再来看其中的一个构造方法LinkedBlockingQueue(Collection<? extends E> c),
/** * Creates a {@code LinkedBlockingQueue} with a capacity of * {@link Integer#MAX_VALUE}, initially containing the elements of the * given collection, * added in traversal order of the collection's iterator. * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null */ public LinkedBlockingQueue(Collection<? extends E> c) { this(Integer.MAX_VALUE); final ReentrantLock putLock = this.putLock; putLock.lock(); // Never contended, but necessary for visibility try { int n = 0; for (E e : c) { if (e == null) throw new NullPointerException();//如果满了就会报异常 if (n == capacity) throw new IllegalStateException("Queue full");//添加 enqueue(new Node<E>(e)); ++n; } count.set(n); } finally { putLock.unlock(); } }put(E e)方法调用的也是enqueue方法,如果满了就会等待。
while (count.get() == capacity) { notFull.await(); } enqueue(node);然后再看下一个方法peek,表示的是返回head节点的下一个节点值,因为head节点是没有值的,
public E peek() { if (count.get() == 0) return null; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { return (count.get() > 0) ? head.next.item : null; } finally { takeLock.unlock(); } }接着看unlink(Node<E> p, Node<E> trail)方法,表示断开p到trail之间的节点,
/** * Unlinks interior Node p with predecessor trail. */ void unlink(Node<E> p, Node<E> trail) { // assert isFullyLocked(); // p.next is not changed, to allow iterators that are // traversing p to maintain their weak-consistency guarantee. p.item = null; trail.next = p.next; if (last == p) last = trail; if (count.getAndDecrement() == capacity) notFull.signal(); }剩下的基本上也没什么可说的。
阅读全文
0 0
- LinkedBlockingQueue源码讲解
- LinkedBlockingQueue源码解析
- LinkedBlockingQueue 源码学习
- LinkedBlockingQueue源码分析
- LinkedBlockingQueue源码阅读
- 源码分析-LinkedBlockingQueue
- LinkedBlockingQueue源码分析
- LinkedBlockingQueue源码解析
- LinkedBlockingQueue源码分析
- LinkedBlockingQueue源码分析
- LinkedBlockingQueue源码分析
- java源码分析09-LinkedBlockingQueue
- java LinkedBlockingQueue源码简析
- LinkedBlockingQueue源码分析(JDK8)
- java源码阅读之LinkedBlockingQueue
- 阻塞队列LinkedBlockingQueue源码分析
- 阻塞队列LinkedBlockingQueue源码分析
- JDK7中LinkedBlockingQueue源码解析
- 【J2ee】session和cookie的区别与联系
- mysql explain执行计划详解
- Odoo的游标cr获取结果fetchall等分析
- 虚拟机的安装
- 坑
- LinkedBlockingQueue源码讲解
- Host管理工具 SwitchHosts
- BZOJ 1068 [SCOI 2007] 区间DP 解题报告
- Vue2.0路由动态路径的绑定
- OpenStack的Ocata版本安装
- Onvif协议初步探究
- C#加密算法HmacSHA256,SHA256
- 20万、50万、100万年薪的算法工程师能力上有哪些差距?
- bzoj3439: Kpm的MC密码