jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 9242 ef138d47df58
child 11279 d9dab5ec5044
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
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: 4110
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: 4110
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: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A bounded {@linkplain BlockingQueue blocking queue} backed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * array.  This queue orders elements FIFO (first-in-first-out).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * queue the longest time.  The <em>tail</em> of the queue is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * element that has been on the queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>This is a classic &quot;bounded buffer&quot;, in which a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * fixed-sized array holds elements inserted by producers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * extracted by consumers.  Once created, the capacity cannot be
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    52
 * changed.  Attempts to {@code put} an element into a full queue
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    53
 * will result in the operation blocking; attempts to {@code take} an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * element from an empty queue will similarly block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    56
 * <p>This class supports an optional fairness policy for ordering
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * waiting producer and consumer threads.  By default, this ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * is not guaranteed. However, a queue constructed with fairness set
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    59
 * to {@code true} grants threads access in FIFO order. Fairness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * generally decreases throughput but reduces variability and avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * starvation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Serialization ID. This class relies on default serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * even for the items array, which is default-serialized, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * it is empty. Otherwise it could not be declared final, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * necessary here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final long serialVersionUID = -817911632652898426L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    86
    /** The queued items */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    87
    final Object[] items;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    88
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    89
    /** items index for next take, poll, peek or remove */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    90
    int takeIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    91
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    92
    /** items index for next put, offer, or add */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    93
    int putIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    94
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    95
    /** Number of elements in the queue */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    96
    int count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Concurrency control uses the classic two-condition algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * found in any textbook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /** Main lock guarding all access */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
    final ReentrantLock lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** Condition for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final Condition notEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /** Condition for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private final Condition notFull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Internal helper methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Circularly increment i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    final int inc(int i) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   116
        return (++i == items.length) ? 0 : i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   117
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   118
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   119
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   120
     * Circularly decrement i.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   121
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   122
    final int dec(int i) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   123
        return ((i == 0) ? items.length : i) - 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   124
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   125
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   126
    @SuppressWarnings("unchecked")
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   127
    static <E> E cast(Object item) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   128
        return (E) item;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   129
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   130
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   131
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   132
     * Returns item at index i.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   133
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   134
    final E itemAt(int i) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   135
        return this.<E>cast(items[i]);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   136
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   137
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   138
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   139
     * Throws NullPointerException if argument is null.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   140
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   141
     * @param v the element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   142
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   143
    private static void checkNotNull(Object v) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   144
        if (v == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   145
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Inserts element at current put position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private void insert(E x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        items[putIndex] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        putIndex = inc(putIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Extracts element at current take position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private E extract() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   164
        final Object[] items = this.items;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   165
        E x = this.<E>cast(items[takeIndex]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        items[takeIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        takeIndex = inc(takeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   174
     * Deletes item at position i.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   175
     * Utility for remove and iterator.remove.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    void removeAt(int i) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   179
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // if removing front item, just advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (i == takeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            items[takeIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            takeIndex = inc(takeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // slide over all others up through putIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                int nexti = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (nexti != putIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    items[i] = items[nexti];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    i = nexti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    putIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   203
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * capacity and default access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   207
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public ArrayBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this(capacity, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   214
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * capacity and the specified access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   218
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *        on insertion or removal, are processed in FIFO order;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   220
     *        if {@code false} the access order is unspecified.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   221
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public ArrayBlockingQueue(int capacity, boolean fair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (capacity <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new IllegalArgumentException();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   226
        this.items = new Object[capacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        lock = new ReentrantLock(fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        notEmpty = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        notFull =  lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   233
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * capacity, the specified access policy and initially containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   239
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *        on insertion or removal, are processed in FIFO order;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   241
     *        if {@code false} the access order is unspecified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param c the collection of elements to initially contain
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   243
     * @throws IllegalArgumentException if {@code capacity} is less than
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   244
     *         {@code c.size()}, or less than 1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public ArrayBlockingQueue(int capacity, boolean fair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                              Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this(capacity, fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   252
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   253
        lock.lock(); // Lock only for visibility, not mutual exclusion
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   254
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   255
            int i = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   256
            try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   257
                for (E e : c) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   258
                    checkNotNull(e);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   259
                    items[i++] = e;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   260
                }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   261
            } catch (ArrayIndexOutOfBoundsException ex) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   262
                throw new IllegalArgumentException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   263
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   264
            count = i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   265
            putIndex = (i == capacity) ? 0 : i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   266
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   267
            lock.unlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   268
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * possible to do so immediately without exceeding the queue's capacity,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   274
     * returning {@code true} upon success and throwing an
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   275
     * {@code IllegalStateException} if this queue is full.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   278
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws IllegalStateException if this queue is full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return super.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * possible to do so immediately without exceeding the queue's capacity,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   289
     * returning {@code true} upon success and {@code false} if this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * is full.  This method is generally preferable to method {@link #add},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * which can fail to insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean offer(E e) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   296
        checkNotNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                insert(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * for space to become available if the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public void put(E e) throws InterruptedException {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   319
        checkNotNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   323
            while (count == items.length)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   324
                notFull.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            insert(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * up to the specified wait time for space to become available if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   342
        checkNotNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   347
            while (count == items.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   350
                nanos = notFull.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   352
            insert(e);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   353
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   363
            return (count == 0) ? null : extract();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   373
            while (count == 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   374
                notEmpty.await();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   375
            return extract();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   386
            while (count == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    return null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   389
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   391
            return extract();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            lock.unlock();
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
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   401
            return (count == 0) ? null : itemAt(takeIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * blocking. This is always equal to the initial capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   430
     * less the current {@code size} of this queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   433
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return items.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Removes a single instance of the specified element from this queue,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   449
     * if it is present.  More formally, removes an element {@code e} such
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   450
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * elements.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   452
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   455
     * <p>Removal of interior elements in circular array based queues
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   456
     * is an intrinsically slow and disruptive operation, so should
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   457
     * be undertaken only in exceptional circumstances, ideally
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   458
     * only when the queue is known not to be accessible by other
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   459
     * threads.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   460
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param o element to be removed from this queue, if present
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   462
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (o == null) return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   466
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   470
            for (int i = takeIndex, k = count; k > 0; i = inc(i), k--) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (o.equals(items[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    removeAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   476
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   483
     * 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
   484
     * 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
   485
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param o object to be checked for containment in this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   488
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (o == null) return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   492
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   496
            for (int i = takeIndex, k = count; k > 0; i = inc(i), k--)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (o.equals(items[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public Object[] toArray() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   519
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   523
            final int count = this.count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            Object[] a = new Object[count];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   525
            for (int i = takeIndex, k = 0; k < count; i = inc(i), k++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   526
                a[k] = items[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * the array immediately following the end of the queue is set to
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   543
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   550
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * The following code can be used to dump the queue into a newly
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   552
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   557
     * Note that {@code toArray(new Object[0])} is identical in function to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   558
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   569
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public <T> T[] toArray(T[] a) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   571
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   575
            final int count = this.count;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   576
            final int len = a.length;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   577
            if (len < count)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                a = (T[])java.lang.reflect.Array.newInstance(
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   579
                    a.getClass().getComponentType(), count);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   580
            for (int i = takeIndex, k = 0; k < count; i = inc(i), k++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   581
                a[k] = (T) items[i];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   582
            if (len > count)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                a[count] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   594
            int k = count;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   595
            if (k == 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   596
                return "[]";
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   597
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   598
            StringBuilder sb = new StringBuilder();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   599
            sb.append('[');
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   600
            for (int i = takeIndex; ; i = inc(i)) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   601
                Object e = items[i];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   602
                sb.append(e == this ? "(this Collection)" : e);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   603
                if (--k == 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   604
                    return sb.append(']').toString();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   605
                sb.append(',').append(' ');
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   606
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public void clear() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   617
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   621
            for (int i = takeIndex, k = count; k > 0; i = inc(i), k--)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            putIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            takeIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public int drainTo(Collection<? super E> c) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   639
        checkNotNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            throw new IllegalArgumentException();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   642
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            int max = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            while (n < max) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   650
                c.add(this.<E>cast(items[i]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                putIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                takeIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public int drainTo(Collection<? super E> c, int maxElements) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   674
        checkNotNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return 0;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   679
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            int n = 0;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   685
            int max = (maxElements < count) ? maxElements : count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            while (n < max) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   687
                c.add(this.<E>cast(items[i]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                count -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                takeIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * 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
   705
     * 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
   706
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   707
     * <p>The returned {@code Iterator} is a "weakly consistent" iterator that
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   708
     * will never throw {@link java.util.ConcurrentModificationException
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   709
     * ConcurrentModificationException},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    public Iterator<E> iterator() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   717
        return new Itr();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   721
     * Iterator for ArrayBlockingQueue. To maintain weak consistency
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   722
     * with respect to puts and takes, we (1) read ahead one slot, so
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   723
     * as to not report hasNext true but then not have an element to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   724
     * return -- however we later recheck this slot to use the most
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   725
     * current value; (2) ensure that each array slot is traversed at
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   726
     * most once (by tracking "remaining" elements); (3) skip over
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   727
     * null slots, which can occur if takes race ahead of iterators.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   728
     * However, for circular array-based queues, we cannot rely on any
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   729
     * well established definition of what it means to be weakly
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   730
     * consistent with respect to interior removes since these may
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   731
     * require slot overwrites in the process of sliding elements to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   732
     * cover gaps. So we settle for resiliency, operating on
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   733
     * established apparent nexts, which may miss some elements that
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   734
     * have moved between calls to next.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private class Itr implements Iterator<E> {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   737
        private int remaining; // Number of elements yet to be returned
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   738
        private int nextIndex; // Index of element to be returned by next
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   739
        private E nextItem;    // Element to be returned by next call to next
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   740
        private E lastItem;    // Element returned by last call to next
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   741
        private int lastRet;   // Index of last element returned, or -1 if none
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        Itr() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   744
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   745
            lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   746
            try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   747
                lastRet = -1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   748
                if ((remaining = count) > 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   749
                    nextItem = itemAt(nextIndex = takeIndex);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   750
            } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   751
                lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        public boolean hasNext() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   756
            return remaining > 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   763
                if (remaining <= 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                lastRet = nextIndex;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   766
                E x = itemAt(nextIndex);  // check for fresher value
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   767
                if (x == null) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   768
                    x = nextItem;         // we are forced to report old value
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   769
                    lastItem = null;      // but ensure remove fails
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   770
                }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   771
                else
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   772
                    lastItem = x;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   773
                while (--remaining > 0 && // skip over nulls
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   774
                       (nextItem = itemAt(nextIndex = inc(nextIndex))) == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   775
                    ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                int i = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                lastRet = -1;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   790
                E x = lastItem;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   791
                lastItem = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   792
                // only remove if item still at index
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   793
                if (x != null && x == items[i]) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   794
                    boolean removingHead = (i == takeIndex);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   795
                    removeAt(i);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   796
                    if (!removingHead)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   797
                        nextIndex = dec(nextIndex);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   798
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   804
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
}