jdk/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java
author dl
Fri, 26 Oct 2012 21:34:24 +0100
changeset 14325 622c473a21aa
parent 11013 27f7a2f3be20
child 18767 6214297bf27d
permissions -rw-r--r--
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012 Reviewed-by: chegar, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7976
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    37
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    38
import java.util.concurrent.atomic.AtomicInteger;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    39
import java.util.concurrent.locks.Condition;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    40
import java.util.concurrent.locks.ReentrantLock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    41
import java.util.AbstractQueue;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    42
import java.util.Collection;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    43
import java.util.Iterator;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    44
import java.util.NoSuchElementException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An optionally-bounded {@linkplain BlockingQueue blocking queue} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This queue orders elements FIFO (first-in-first-out).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * queue the longest time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The <em>tail</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Linked queues typically have higher throughput than array-based queues but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * less predictable performance in most concurrent applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> The optional capacity bound constructor argument serves as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * way to prevent excessive queue expansion. The capacity, if unspecified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * is equal to {@link Integer#MAX_VALUE}.  Linked nodes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * dynamically created upon each insertion unless this would bring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * queue above capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final long serialVersionUID = -6903933977591709194L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * A variant of the "two lock queue" algorithm.  The putLock gates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * entry to put (and offer), and has an associated condition for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * waiting puts.  Similarly for the takeLock.  The "count" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * that they both rely on is maintained as an atomic to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * needing to get both locks in most cases. Also, to minimize need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * for puts to get takeLock and vice-versa, cascading notifies are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * used. When a put notices that it has enabled at least one take,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * it signals taker. That taker in turn signals others if more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * items have been entered since the signal. And symmetrically for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * takes signalling puts. Operations such as remove(Object) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * iterators acquire both locks.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    93
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    94
     * Visibility between writers and readers is provided as follows:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    95
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    96
     * Whenever an element is enqueued, the putLock is acquired and
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    97
     * count updated.  A subsequent reader guarantees visibility to the
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    98
     * enqueued Node by either acquiring the putLock (via fullyLock)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    99
     * or by acquiring the takeLock, and then reading n = count.get();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   100
     * this gives visibility to the first n items.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   101
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   102
     * To implement weakly consistent iterators, it appears we need to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   103
     * keep all Nodes GC-reachable from a predecessor dequeued Node.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   104
     * That would cause two problems:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   105
     * - allow a rogue Iterator to cause unbounded memory retention
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   106
     * - cause cross-generational linking of old Nodes to new Nodes if
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   107
     *   a Node was tenured while live, which generational GCs have a
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   108
     *   hard time dealing with, causing repeated major collections.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   109
     * However, only non-deleted Nodes need to be reachable from
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   110
     * dequeued Nodes, and reachability does not necessarily have to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   111
     * be of the kind understood by the GC.  We use the trick of
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   112
     * linking a Node that has just been dequeued to itself.  Such a
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   113
     * self-link implicitly means to advance to head.next.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Linked list node class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static class Node<E> {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   120
        E item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   121
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   122
        /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   123
         * One of:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   124
         * - the real successor Node
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   125
         * - this Node, meaning the successor is head.next
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   126
         * - null, meaning there is no successor (this is the last node)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   127
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Node<E> next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Node(E x) { item = x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /** The capacity bound, or Integer.MAX_VALUE if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private final int capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** Current number of elements */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   137
    private final AtomicInteger count = new AtomicInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   139
    /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   140
     * Head of linked list.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   141
     * Invariant: head.item == null
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   142
     */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   143
    transient Node<E> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   145
    /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   146
     * Tail of linked list.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   147
     * Invariant: last.next == null
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   148
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private transient Node<E> last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /** Lock held by take, poll, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private final ReentrantLock takeLock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /** Wait queue for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private final Condition notEmpty = takeLock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /** Lock held by put, offer, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private final ReentrantLock putLock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /** Wait queue for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private final Condition notFull = putLock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Signals a waiting take. Called only from put/offer (which do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * otherwise ordinarily lock takeLock.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private void signalNotEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Signals a waiting put. Called only from take/poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private void signalNotFull() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   191
     * Links node at end of queue.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   192
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   193
     * @param node the node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   195
    private void enqueue(Node<E> node) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   196
        // assert putLock.isHeldByCurrentThread();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   197
        // assert last.next == null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   198
        last = last.next = node;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   202
     * Removes a node from head of queue.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   203
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   206
    private E dequeue() {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   207
        // assert takeLock.isHeldByCurrentThread();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   208
        // assert head.item == null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   209
        Node<E> h = head;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   210
        Node<E> first = h.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   211
        h.next = h; // help GC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        head = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        E x = first.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        first.item = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Lock to prevent both puts and takes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   221
    void fullyLock() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Unlock to allow both puts and takes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   229
    void fullyUnlock() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   234
//     /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   235
//      * Tells whether both locks are held by current thread.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   236
//      */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   237
//     boolean isFullyLocked() {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   238
//         return (putLock.isHeldByCurrentThread() &&
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   239
//                 takeLock.isHeldByCurrentThread());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   240
//     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   243
     * Creates a {@code LinkedBlockingQueue} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * {@link Integer#MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public LinkedBlockingQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        this(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   251
     * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param capacity the capacity of this queue
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   254
     * @throws IllegalArgumentException if {@code capacity} is not greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public LinkedBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (capacity <= 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        this.capacity = capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        last = head = new Node<E>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   264
     * Creates a {@code LinkedBlockingQueue} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * {@link Integer#MAX_VALUE}, initially containing the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public LinkedBlockingQueue(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this(Integer.MAX_VALUE);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   275
        final ReentrantLock putLock = this.putLock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   276
        putLock.lock(); // Never contended, but necessary for visibility
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   277
        try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   278
            int n = 0;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   279
            for (E e : c) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   280
                if (e == null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   281
                    throw new NullPointerException();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   282
                if (n == capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   283
                    throw new IllegalStateException("Queue full");
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   284
                enqueue(new Node<E>(e));
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   285
                ++n;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   286
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   287
            count.set(n);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   288
        } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   289
            putLock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   290
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * blocking. This is always equal to the initial capacity of this queue
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   310
     * less the current {@code size} of this queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   313
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return capacity - count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Inserts the specified element at the tail of this queue, waiting if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * necessary for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void put(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (e == null) throw new NullPointerException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   330
        // Note: convention in all put/take/etc is to preset local var
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   331
        // holding count negative to indicate failure unless set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int c = -1;
11013
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   333
        Node<E> node = new Node<E>(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        putLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
             * Note that count is used in wait guard even though it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
             * not protected by lock. This works because count can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
             * only decrease at this point (all other puts are shut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
             * out by lock), and we (or some other waiting put) are
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   343
             * signalled if it ever changes from capacity. Similarly
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   344
             * for all other uses of count in other wait guards.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
             */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   346
            while (count.get() == capacity) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   347
                notFull.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   349
            enqueue(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            c = count.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (c + 1 < capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            signalNotEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Inserts the specified element at the tail of this queue, waiting if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * necessary up to the specified wait time for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   364
     * @return {@code true} if successful, or {@code false} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         the specified waiting time elapses before space is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        putLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   379
            while (count.get() == capacity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    return false;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   382
                nanos = notFull.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   384
            enqueue(new Node<E>(e));
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   385
            c = count.getAndIncrement();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   386
            if (c + 1 < capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   387
                notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            signalNotEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * possible to do so immediately without exceeding the queue's capacity,
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   399
     * returning {@code true} upon success and {@code false} if this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * When using a capacity-restricted queue, this method is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * preferable to method {@link BlockingQueue#add add}, which can fail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (count.get() == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int c = -1;
11013
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   413
        Node<E> node = new Node<E>(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (count.get() < capacity) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   418
                enqueue(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                c = count.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (c + 1 < capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            signalNotEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return c >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        takeLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   438
            while (count.get() == 0) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   439
                notEmpty.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   441
            x = dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            c = count.getAndDecrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (c > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        E x = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        takeLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   461
            while (count.get() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    return null;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   464
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   466
            x = dequeue();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   467
            c = count.getAndDecrement();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   468
            if (c > 1)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   469
                notEmpty.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (count.get() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        E x = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (count.get() > 0) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   488
                x = dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                c = count.getAndDecrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (c > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (count.get() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            Node<E> first = head.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return first.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   518
     * Unlinks interior Node p with predecessor trail.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   519
     */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   520
    void unlink(Node<E> p, Node<E> trail) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   521
        // assert isFullyLocked();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   522
        // p.next is not changed, to allow iterators that are
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   523
        // traversing p to maintain their weak-consistency guarantee.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   524
        p.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   525
        trail.next = p.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   526
        if (last == p)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   527
            last = trail;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   528
        if (count.getAndDecrement() == capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   529
            notFull.signal();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   530
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   531
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   532
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Removes a single instance of the specified element from this queue,
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   534
     * if it is present.  More formally, removes an element {@code e} such
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   535
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * elements.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   537
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param o element to be removed from this queue, if present
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   541
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   547
            for (Node<E> trail = head, p = trail.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   548
                 p != null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   549
                 trail = p, p = p.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (o.equals(p.item)) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   551
                    unlink(p, trail);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   552
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   555
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   562
     * Returns {@code true} if this queue contains the specified element.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   563
     * More formally, returns {@code true} if and only if this queue contains
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   564
     * at least one element {@code e} such that {@code o.equals(e)}.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   565
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   566
     * @param o object to be checked for containment in this queue
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   567
     * @return {@code true} if this queue contains the specified element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   568
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   569
    public boolean contains(Object o) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   570
        if (o == null) return false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   571
        fullyLock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   572
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   573
            for (Node<E> p = head.next; p != null; p = p.next)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   574
                if (o.equals(p.item))
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   575
                    return true;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   576
            return false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   577
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   578
            fullyUnlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   579
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   580
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   581
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   582
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int size = count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            Object[] a = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            for (Node<E> p = head.next; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                a[k++] = p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * the array immediately following the end of the queue is set to
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   619
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   626
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * The following code can be used to dump the queue into a newly
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   628
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   630
     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   632
     * Note that {@code toArray(new Object[0])} is identical in function to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   633
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   644
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            int size = count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                a = (T[])java.lang.reflect.Array.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    (a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            int k = 0;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   654
            for (Node<E> p = head.next; p != null; p = p.next)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                a[k++] = (T)p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if (a.length > k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                a[k] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   667
            Node<E> p = head.next;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   668
            if (p == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   669
                return "[]";
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   670
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   671
            StringBuilder sb = new StringBuilder();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   672
            sb.append('[');
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   673
            for (;;) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   674
                E e = p.item;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   675
                sb.append(e == this ? "(this Collection)" : e);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   676
                p = p.next;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   677
                if (p == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   678
                    return sb.append(']').toString();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   679
                sb.append(',').append(' ');
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   680
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   693
            for (Node<E> p, h = head; (p = h.next) != null; h = p) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   694
                h.next = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   695
                p.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   696
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   697
            head = last;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   698
            // assert head.item == null && head.next == null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (count.getAndSet(0) == capacity)
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   700
                notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public int drainTo(Collection<? super E> c) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   713
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            throw new IllegalArgumentException();
11013
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   727
        if (maxElements <= 0)
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   728
            return 0;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   729
        boolean signalNotFull = false;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   730
        final ReentrantLock takeLock = this.takeLock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   731
        takeLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   733
            int n = Math.min(maxElements, count.get());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   734
            // count.get provides visibility to first n Nodes
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   735
            Node<E> h = head;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   736
            int i = 0;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   737
            try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   738
                while (i < n) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   739
                    Node<E> p = h.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   740
                    c.add(p.item);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   741
                    p.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   742
                    h.next = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   743
                    h = p;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   744
                    ++i;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   745
                }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   746
                return n;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   747
            } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   748
                // Restore invariants even if c.add() threw
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   749
                if (i > 0) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   750
                    // assert h.item == null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   751
                    head = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   752
                    signalNotFull = (count.getAndAdd(-i) == capacity);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   753
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   756
            takeLock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   757
            if (signalNotFull)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   758
                signalNotFull();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Returns an iterator over the elements in this queue in proper sequence.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   764
     * The elements will be returned in order from first (head) to last (tail).
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   765
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   766
     * <p>The returned iterator is a "weakly consistent" iterator that
3419
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3415
diff changeset
   767
     * will never throw {@link java.util.ConcurrentModificationException
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   768
     * ConcurrentModificationException}, and guarantees to traverse
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   769
     * elements as they existed upon construction of the iterator, and
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   770
     * may (but is not guaranteed to) reflect any modifications
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   771
     * subsequent to construction.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    public Iterator<E> iterator() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   776
        return new Itr();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        /*
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   781
         * Basic weakly-consistent iterator.  At all times hold the next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * item to hand out so that if hasNext() reports true, we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * still have it to return even if lost race with a take etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        private Node<E> current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        private Node<E> lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        private E currentElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        Itr() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   790
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                current = head.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                if (current != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    currentElement = current.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   796
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return current != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   804
        /**
3707
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   805
         * Returns the next live successor of p, or null if no such.
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   806
         *
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   807
         * Unlike other traversal methods, iterators need to handle both:
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   808
         * - dequeued nodes (p.next == p)
3707
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   809
         * - (possibly multiple) interior removed nodes (p.item == null)
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   810
         */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   811
        private Node<E> nextNode(Node<E> p) {
3707
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   812
            for (;;) {
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   813
                Node<E> s = p.next;
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   814
                if (s == p)
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   815
                    return head.next;
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   816
                if (s == null || s.item != null)
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   817
                    return s;
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   818
                p = s;
67e8505ef721 6871697: LinkedBlockingQueue Iterator/remove/poll race
dl
parents: 3419
diff changeset
   819
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   820
        }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   821
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        public E next() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   823
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                if (current == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                E x = currentElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                lastRet = current;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   829
                current = nextNode(current);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   830
                currentElement = (current == null) ? null : current.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   833
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (lastRet == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                throw new IllegalStateException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   840
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                Node<E> node = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                lastRet = null;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   844
                for (Node<E> trail = head, p = trail.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   845
                     p != null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   846
                     trail = p, p = p.next) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   847
                    if (p == node) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   848
                        unlink(p, trail);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   849
                        break;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   850
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   853
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   859
     * Saves this queue to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @serialData The capacity is emitted (int), followed by all of
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   862
     * its elements (each an {@code Object}) in the proper order,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * followed by a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // Write out any hidden stuff, plus capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // Write out all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            for (Node<E> p = head.next; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                s.writeObject(p.item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   885
     * Reconstitutes this queue from a stream (that is, deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // Read in capacity, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        count.set(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        last = head = new Node<E>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // Read in all elements and place in queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        for (;;) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   897
            @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            E item = (E)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
}