src/share/classes/java/util/concurrent/LinkedBlockingQueue.java

Print this page

        

*** 187,204 **** putLock.unlock(); } } /** ! * Creates a node and links it at end of queue. * ! * @param x the item */ ! private void enqueue(E x) { // assert putLock.isHeldByCurrentThread(); // assert last.next == null; ! last = last.next = new Node<E>(x); } /** * Removes a node from head of queue. * --- 187,204 ---- putLock.unlock(); } } /** ! * Links node at end of queue. * ! * @param node the node */ ! private void enqueue(Node<E> node) { // assert putLock.isHeldByCurrentThread(); // assert last.next == null; ! last = last.next = node; } /** * Removes a node from head of queue. *
*** 280,290 **** for (E e : c) { if (e == null) throw new NullPointerException(); if (n == capacity) throw new IllegalStateException("Queue full"); ! enqueue(e); ++n; } count.set(n); } finally { putLock.unlock(); --- 280,290 ---- 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();
*** 330,339 **** --- 330,340 ---- public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); // Note: convention in all put/take/etc is to preset local var // holding count negative to indicate failure unless set. int c = -1; + Node<E> node = new Node(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { /*
*** 345,355 **** * for all other uses of count in other wait guards. */ while (count.get() == capacity) { notFull.await(); } ! enqueue(e); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); --- 346,356 ---- * for all other uses of count in other wait guards. */ while (count.get() == capacity) { notFull.await(); } ! enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock();
*** 380,390 **** while (count.get() == capacity) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } ! enqueue(e); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); --- 381,391 ---- while (count.get() == capacity) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } ! enqueue(new Node<E>(e)); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock();
*** 409,423 **** if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { ! enqueue(e); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally { --- 410,425 ---- if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; + Node<E> node = new Node(e); final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { ! enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally {
*** 558,567 **** --- 560,590 ---- fullyUnlock(); } } /** + * Returns {@code true} if this queue contains the specified element. + * More formally, returns {@code true} if and only if this queue contains + * at least one element {@code e} such that {@code o.equals(e)}. + * + * @param o object to be checked for containment in this queue + * @return {@code true} if this queue contains the specified element + */ + public boolean contains(Object o) { + if (o == null) return false; + fullyLock(); + try { + for (Node<E> p = head.next; p != null; p = p.next) + if (o.equals(p.item)) + return true; + return false; + } finally { + fullyUnlock(); + } + } + + /** * Returns an array containing all of the elements in this queue, in * proper sequence. * * <p>The returned array will be "safe" in that no references to it are * maintained by this queue. (In other words, this method must allocate
*** 643,653 **** } public String toString() { fullyLock(); try { ! return super.toString(); } finally { fullyUnlock(); } } --- 666,689 ---- } public String toString() { fullyLock(); try { ! Node<E> p = head.next; ! if (p == null) ! return "[]"; ! ! StringBuilder sb = new StringBuilder(); ! sb.append('['); ! for (;;) { ! E e = p.item; ! sb.append(e == this ? "(this Collection)" : e); ! p = p.next; ! if (p == null) ! return sb.append(']').toString(); ! sb.append(',').append(' '); ! } } finally { fullyUnlock(); } }
*** 725,740 **** } } /** * Returns an iterator over the elements in this queue in proper sequence. ! * The returned {@code Iterator} is a "weakly consistent" iterator that * will never throw {@link java.util.ConcurrentModificationException ! * ConcurrentModificationException}, ! * and guarantees to traverse elements as they existed upon ! * construction of the iterator, and may (but is not guaranteed to) ! * reflect any modifications subsequent to construction. * * @return an iterator over the elements in this queue in proper sequence */ public Iterator<E> iterator() { return new Itr(); --- 761,778 ---- } } /** * Returns an iterator over the elements in this queue in proper sequence. ! * The elements will be returned in order from first (head) to last (tail). ! * ! * <p>The returned iterator is a "weakly consistent" iterator that * will never throw {@link java.util.ConcurrentModificationException ! * ConcurrentModificationException}, and guarantees to traverse ! * elements as they existed upon construction of the iterator, and ! * may (but is not guaranteed to) reflect any modifications ! * subsequent to construction. * * @return an iterator over the elements in this queue in proper sequence */ public Iterator<E> iterator() { return new Itr();