java 遍历linkedblockingqueue_Java多线程系列--“JUC集合”08之 LinkedBlockingQueue

1 /*

2 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.3 *4 *5 *6 *7 *8 *9 *10 *11 *12 *13 *14 *15 *16 *17 *18 *19 *20 *21 *22 *23 */

24

25 /*

26 *27 *28 *29 *30 *31 * Written by Doug Lea with assistance from members of JCP JSR-16632 * Expert Group and released to the public domain, as explained at33 *http://creativecommons.org/publicdomain/zero/1.0/

34 */

35

36 packagejava.util.concurrent;37

38 importjava.util.concurrent.atomic.AtomicInteger;39 importjava.util.concurrent.locks.Condition;40 importjava.util.concurrent.locks.ReentrantLock;41 importjava.util.AbstractQueue;42 importjava.util.Collection;43 importjava.util.Iterator;44 importjava.util.NoSuchElementException;45

46 /**

47 * An optionally-bounded {@linkplainBlockingQueue blocking queue} based on48 * linked nodes.49 * This queue orders elements FIFO (first-in-first-out).50 * The head of the queue is that element that has been on the51 * queue the longest time.52 * The tail of the queue is that element that has been on the53 * queue the shortest time. New elements54 * are inserted at the tail of the queue, and the queue retrieval55 * operations obtain elements at the head of the queue.56 * Linked queues typically have higher throughput than array-based queues but57 * less predictable performance in most concurrent applications.58 *59 *

The optional capacity bound constructor argument serves as a60 * way to prevent excessive queue expansion. The capacity, if unspecified,61 * is equal to {@linkInteger#MAX_VALUE}. Linked nodes are62 * dynamically created upon each insertion unless this would bring the63 * queue above capacity.64 *65 *

This class and its iterator implement all of the66 * optional methods of the {@linkCollection} and {@link

67 * Iterator} interfaces.68 *69 *

This class is a member of the70 * 71 * Java Collections Framework.72 *73 *@since1.574 *@authorDoug Lea75 *@param the type of elements held in this collection76 *77 */

78 public class LinkedBlockingQueue extends AbstractQueue

79 implements BlockingQueue, java.io.Serializable {80 private static final long serialVersionUID = -6903933977591709194L;81

82 /*

83 * A variant of the "two lock queue" algorithm. The putLock gates84 * entry to put (and offer), and has an associated condition for85 * waiting puts. Similarly for the takeLock. The "count" field86 * that they both rely on is maintained as an atomic to avoid87 * needing to get both locks in most cases. Also, to minimize need88 * for puts to get takeLock and vice-versa, cascading notifies are89 * used. When a put notices that it has enabled at least one take,90 * it signals taker. That taker in turn signals others if more91 * items have been entered since the signal. And symmetrically for92 * takes signalling puts. Operations such as remove(Object) and93 * iterators acquire both locks.94 *95 * Visibility between writers and readers is provided as follows:96 *97 * Whenever an element is enqueued, the putLock is acquired and98 * count updated. A subsequent reader guarantees visibility to the99 * enqueued Node by either acquiring the putLock (via fullyLock)100 * or by acquiring the takeLock, and then reading n = count.get();101 * this gives visibility to the first n items.102 *103 * To implement weakly consistent iterators, it appears we need to104 * keep all Nodes GC-reachable from a predecessor dequeued Node.105 * That would cause two problems:106 * - allow a rogue Iterator to cause unbounded memory retention107 * - cause cross-generational linking of old Nodes to new Nodes if108 * a Node was tenured while live, which generational GCs have a109 * hard time dealing with, causing repeated major collections.110 * However, only non-deleted Nodes need to be reachable from111 * dequeued Nodes, and reachability does not necessarily have to112 * be of the kind understood by the GC. We use the trick of113 * linking a Node that has just been dequeued to itself. Such a114 * self-link implicitly means to advance to head.next.115 */

116

117 /**

118 * Linked list node class119 */

120 static class Node{121 E item;122

123 /**

124 * One of:125 * - the real successor Node126 * - this Node, meaning the successor is head.next127 * - null, meaning there is no successor (this is the last node)128 */

129 Nodenext;130

131 Node(E x) { item =x; }132 }133

134 /**The capacity bound, or Integer.MAX_VALUE if none*/

135 private final intcapacity;136

137 /**Current number of elements*/

138 private final AtomicInteger count = new AtomicInteger(0);139

140 /**

141 * Head of linked list.142 * Invariant: head.item == null143 */

144 private transient Nodehead;145

146 /**

147 * Tail of linked list.148 * Invariant: last.next == null149 */

150 private transient Nodelast;151

152 /**Lock held by take, poll, etc*/

153 private final ReentrantLock takeLock = newReentrantLock();154

155 /**Wait queue for waiting takes*/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值