jdk/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java
author dl
Mon, 28 Nov 2016 23:36:11 -0800
changeset 42319 0193886267c3
parent 32991 b27c76b82713
child 42927 1d31e540bfcb
permissions -rw-r--r--
8143577: optimize ArrayList.removeIf 8169679: ArrayList.subList().iterator().forEachRemaining() off-by-one-error 8167202: ArrayDeque improvements 8164793: new ArrayDeque(2**N) allocates backing array of size 2**(N+1) 8169739: LinkedBlockingDeque spliterator needs to support node self-linking 8169738: CopyOnWriteArrayList subList needs more synchronization Reviewed-by: martin, smarks, psandoz, forax
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;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    38
import java.lang.ref.WeakReference;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    39
import java.util.AbstractQueue;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    40
import java.util.Arrays;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    41
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    42
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    43
import java.util.NoSuchElementException;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    44
import java.util.Objects;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    45
import java.util.Spliterator;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    46
import java.util.Spliterators;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    47
import java.util.concurrent.locks.Condition;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    48
import java.util.concurrent.locks.ReentrantLock;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    49
import java.util.function.Consumer;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    50
import java.util.function.Predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A bounded {@linkplain BlockingQueue blocking queue} backed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * array.  This queue orders elements FIFO (first-in-first-out).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * queue the longest time.  The <em>tail</em> of the queue is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * element that has been on the queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>This is a classic &quot;bounded buffer&quot;, in which a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * fixed-sized array holds elements inserted by producers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * 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
    64
 * 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
    65
 * will result in the operation blocking; attempts to {@code take} an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * element from an empty queue will similarly block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    68
 * <p>This class supports an optional fairness policy for ordering
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * waiting producer and consumer threads.  By default, this ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * 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
    71
 * to {@code true} grants threads access in FIFO order. Fairness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * generally decreases throughput but reduces variability and avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * starvation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    85
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    90
    /*
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    91
     * Much of the implementation mechanics, especially the unusual
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    92
     * nested loops, are shared and co-maintained with ArrayDeque.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    93
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Serialization ID. This class relies on default serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * even for the items array, which is default-serialized, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * it is empty. Otherwise it could not be declared final, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * necessary here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final long serialVersionUID = -817911632652898426L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
    /** The queued items */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
    final Object[] items;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   105
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   106
    /** 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
   107
    int takeIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   108
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   109
    /** 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
   110
    int putIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   111
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   112
    /** Number of elements in the queue */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   113
    int count;
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
     * Concurrency control uses the classic two-condition algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * found in any textbook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** Main lock guarding all access */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   121
    final ReentrantLock lock;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /** Condition for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final Condition notEmpty;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /** Condition for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private final Condition notFull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   129
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   130
     * Shared state for currently active iterators, or null if there
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   131
     * are known not to be any.  Allows queue operations to update
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   132
     * iterator state.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   133
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   134
    transient Itrs itrs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   136
    // Internal helper methods
7976
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
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   139
     * Increments i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   140
     * Precondition and postcondition: 0 <= i < modulus.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   141
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   142
    static final int inc(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   143
        if (++i >= modulus) i = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   144
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   145
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   146
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   147
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   148
     * Decrements i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   149
     * Precondition and postcondition: 0 <= i < modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   150
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   151
    static final int dec(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   152
        if (--i < 0) i = modulus - 1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   153
        return i;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   154
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   155
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   156
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   157
     * Returns item at index i.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   158
     */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   159
    @SuppressWarnings("unchecked")
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   160
    final E itemAt(int i) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   161
        return (E) items[i];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   162
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   163
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   164
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   165
     * Returns element at array index i.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   166
     * This is a slight abuse of generics, accepted by javac.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   167
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   168
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   169
    static <E> E itemAt(Object[] items, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   170
        return (E) items[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   171
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   172
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   173
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Inserts element at current put position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   177
    private void enqueue(E e) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   178
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   179
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   180
        // assert items[putIndex] == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   181
        final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   182
        items[putIndex] = e;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   183
        if (++putIndex == items.length) putIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   184
        count++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Extracts element at current take position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   192
    private E dequeue() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   193
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   194
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   195
        // assert items[takeIndex] != null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   196
        final Object[] items = this.items;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   197
        @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   198
        E e = (E) items[takeIndex];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        items[takeIndex] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   200
        if (++takeIndex == items.length) takeIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   201
        count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   202
        if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   203
            itrs.elementDequeued();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        notFull.signal();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   205
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   209
     * Deletes item at array index removeIndex.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   210
     * Utility for remove(Object) and iterator.remove.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   213
    void removeAt(final int removeIndex) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   214
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   215
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   216
        // assert items[removeIndex] != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   217
        // assert removeIndex >= 0 && removeIndex < items.length;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   218
        final Object[] items = this.items;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   219
        if (removeIndex == takeIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   220
            // removing front item; just advance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            items[takeIndex] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   222
            if (++takeIndex == items.length) takeIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   223
            count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   224
            if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   225
                itrs.elementDequeued();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   227
            // an "interior" remove
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   228
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // slide over all others up through putIndex.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   230
            for (int i = removeIndex, putIndex = this.putIndex;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   231
                int pred = i;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   232
                if (++i == items.length) i = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   233
                if (i == putIndex) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   234
                    items[pred] = null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   235
                    this.putIndex = pred;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   238
                items[pred] = items[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   240
            count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   241
            if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   242
                itrs.removedAt(removeIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   248
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * capacity and default access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @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
   252
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public ArrayBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this(capacity, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   259
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * capacity and the specified access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @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
   263
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *        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
   265
     *        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
   266
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public ArrayBlockingQueue(int capacity, boolean fair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (capacity <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new IllegalArgumentException();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   271
        this.items = new Object[capacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        lock = new ReentrantLock(fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        notEmpty = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        notFull =  lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   278
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * capacity, the specified access policy and initially containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @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
   284
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *        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
   286
     *        if {@code false} the access order is unspecified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @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
   288
     * @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
   289
     *         {@code c.size()}, or less than 1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public ArrayBlockingQueue(int capacity, boolean fair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                              Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        this(capacity, fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   297
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   298
        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
   299
        try {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   300
            final Object[] items = this.items;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   301
            int i = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   302
            try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   303
                for (E e : c)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   304
                    items[i++] = Objects.requireNonNull(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   305
            } catch (ArrayIndexOutOfBoundsException ex) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   306
                throw new IllegalArgumentException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   307
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   308
            count = i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   309
            putIndex = (i == capacity) ? 0 : i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   310
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   311
            lock.unlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   312
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * 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
   318
     * 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
   319
     * {@code IllegalStateException} if this queue is full.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   322
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws IllegalStateException if this queue is full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return super.add(e);
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
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * 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
   333
     * returning {@code true} upon success and {@code false} if this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * is full.  This method is generally preferable to method {@link #add},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * which can fail to insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public boolean offer(E e) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   340
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   347
                enqueue(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * for space to become available if the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void put(E e) throws InterruptedException {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   363
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   367
            while (count == items.length)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   368
                notFull.await();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   369
            enqueue(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * up to the specified wait time for space to become available if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   386
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   391
            while (count == items.length) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   392
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   394
                nanos = notFull.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   396
            enqueue(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   397
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   407
            return (count == 0) ? null : dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   417
            while (count == 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   418
                notEmpty.await();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   419
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   430
            while (count == 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   431
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    return null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   433
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   435
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   445
            return itemAt(takeIndex); // null when queue is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * 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
   474
     * less the current {@code size} of this queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <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
   477
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            return items.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * 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
   493
     * 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
   494
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * elements.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   496
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   499
     * <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
   500
     * 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
   501
     * be undertaken only in exceptional circumstances, ideally
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   502
     * 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
   503
     * threads.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   504
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @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
   506
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   513
            if (count > 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   514
                final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   515
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   516
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   517
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   518
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   519
                        if (o.equals(items[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   520
                            removeAt(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   521
                            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   522
                        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   523
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   524
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   526
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   533
     * 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
   534
     * 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
   535
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @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
   538
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   545
            if (count > 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   546
                final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   547
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   548
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   549
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   550
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   551
                        if (o.equals(items[i]))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   552
                            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   553
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   554
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   555
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   579
            final Object[] items = this.items;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   580
            final int end = takeIndex + count;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   581
            final Object[] a = Arrays.copyOfRange(items, takeIndex, end);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   582
            if (end != putIndex)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   583
                System.arraycopy(items, 0, a, items.length - takeIndex, putIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   584
            return a;
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * 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
   600
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   607
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * 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
   609
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   611
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   613
     * 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
   614
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   625
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   630
            final Object[] items = this.items;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   631
            final int count = this.count;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   632
            final int firstLeg = Math.min(items.length - takeIndex, count);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   633
            if (a.length < count) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   634
                a = (T[]) Arrays.copyOfRange(items, takeIndex, takeIndex + count,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   635
                                             a.getClass());
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   636
            } else {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   637
                System.arraycopy(items, takeIndex, a, 0, firstLeg);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   638
                if (a.length > count)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   639
                    a[count] = null;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   640
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   641
            if (firstLeg < count)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   642
                System.arraycopy(items, 0, a, firstLeg, putIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   643
            return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public String toString() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   650
        return Helpers.collectionToString(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        try {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   661
            int k;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   662
            if ((k = count) > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   663
                circularClear(items, takeIndex, putIndex);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   664
                takeIndex = putIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   665
                count = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   666
                if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   667
                    itrs.queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   668
                for (; k > 0 && lock.hasWaiters(notFull); k--)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   669
                    notFull.signal();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   670
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   677
     * Nulls out slots starting at array index i, upto index end.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   678
     * If i == end, the entire array is cleared!
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   679
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   680
    private static void circularClear(Object[] items, int i, int end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   681
        for (int to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   682
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   683
            Arrays.fill(items, i, to, null);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   684
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   685
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   686
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   687
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   688
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public int drainTo(Collection<? super E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   695
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public int drainTo(Collection<? super E> c, int maxElements) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   705
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return 0;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   710
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   714
            int n = Math.min(maxElements, count);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   715
            int take = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   716
            int i = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   717
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   718
                while (i < n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   719
                    @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   720
                    E e = (E) items[take];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   721
                    c.add(e);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   722
                    items[take] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   723
                    if (++take == items.length) take = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   724
                    i++;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   725
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   726
                return n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   727
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   728
                // Restore invariants even if c.add() threw
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   729
                if (i > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   730
                    count -= i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   731
                    takeIndex = take;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   732
                    if (itrs != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   733
                        if (count == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   734
                            itrs.queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   735
                        else if (i > take)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   736
                            itrs.takeIndexWrapped();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   737
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   738
                    for (; i > 0 && lock.hasWaiters(notFull); i--)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   739
                        notFull.signal();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   740
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * 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
   749
     * 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
   750
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   751
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   752
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public Iterator<E> iterator() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   757
        return new Itr();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   761
     * Shared data between iterators and their queue, allowing queue
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   762
     * modifications to update iterators when elements are removed.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   763
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   764
     * This adds a lot of complexity for the sake of correctly
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   765
     * handling some uncommon operations, but the combination of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   766
     * circular-arrays and supporting interior removes (i.e., those
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   767
     * not at head) would cause iterators to sometimes lose their
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   768
     * places and/or (re)report elements they shouldn't.  To avoid
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   769
     * this, when a queue has one or more iterators, it keeps iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   770
     * state consistent by:
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   771
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   772
     * (1) keeping track of the number of "cycles", that is, the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   773
     *     number of times takeIndex has wrapped around to 0.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   774
     * (2) notifying all iterators via the callback removedAt whenever
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   775
     *     an interior element is removed (and thus other elements may
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   776
     *     be shifted).
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   777
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   778
     * These suffice to eliminate iterator inconsistencies, but
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   779
     * unfortunately add the secondary responsibility of maintaining
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   780
     * the list of iterators.  We track all active iterators in a
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   781
     * simple linked list (accessed only when the queue's lock is
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   782
     * held) of weak references to Itr.  The list is cleaned up using
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   783
     * 3 different mechanisms:
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   784
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   785
     * (1) Whenever a new iterator is created, do some O(1) checking for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   786
     *     stale list elements.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   787
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   788
     * (2) Whenever takeIndex wraps around to 0, check for iterators
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   789
     *     that have been unused for more than one wrap-around cycle.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   790
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   791
     * (3) Whenever the queue becomes empty, all iterators are notified
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   792
     *     and this entire data structure is discarded.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   793
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   794
     * So in addition to the removedAt callback that is necessary for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   795
     * correctness, iterators have the shutdown and takeIndexWrapped
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   796
     * callbacks that help remove stale iterators from the list.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   797
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   798
     * Whenever a list element is examined, it is expunged if either
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   799
     * the GC has determined that the iterator is discarded, or if the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   800
     * iterator reports that it is "detached" (does not need any
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   801
     * further state updates).  Overhead is maximal when takeIndex
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   802
     * never advances, iterators are discarded before they are
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   803
     * exhausted, and all removals are interior removes, in which case
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   804
     * all stale iterators are discovered by the GC.  But even in this
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   805
     * case we don't increase the amortized complexity.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   806
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   807
     * Care must be taken to keep list sweeping methods from
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   808
     * reentrantly invoking another such method, causing subtle
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   809
     * corruption bugs.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   810
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   811
    class Itrs {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   812
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   813
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   814
         * Node in a linked list of weak iterator references.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   815
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   816
        private class Node extends WeakReference<Itr> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   817
            Node next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   818
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   819
            Node(Itr iterator, Node next) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   820
                super(iterator);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   821
                this.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   822
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   823
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   824
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   825
        /** Incremented whenever takeIndex wraps around to 0 */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   826
        int cycles;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   827
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   828
        /** Linked list of weak iterator references */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   829
        private Node head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   830
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   831
        /** Used to expunge stale iterators */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   832
        private Node sweeper;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   833
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   834
        private static final int SHORT_SWEEP_PROBES = 4;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   835
        private static final int LONG_SWEEP_PROBES = 16;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   836
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   837
        Itrs(Itr initial) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   838
            register(initial);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   839
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   840
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   841
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   842
         * Sweeps itrs, looking for and expunging stale iterators.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   843
         * If at least one was found, tries harder to find more.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   844
         * Called only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   845
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   846
         * @param tryHarder whether to start in try-harder mode, because
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   847
         * there is known to be at least one iterator to collect
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   848
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   849
        void doSomeSweeping(boolean tryHarder) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   850
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   851
            // assert head != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   852
            int probes = tryHarder ? LONG_SWEEP_PROBES : SHORT_SWEEP_PROBES;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   853
            Node o, p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   854
            final Node sweeper = this.sweeper;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   855
            boolean passedGo;   // to limit search to one full sweep
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   856
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   857
            if (sweeper == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   858
                o = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   859
                p = head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   860
                passedGo = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   861
            } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   862
                o = sweeper;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   863
                p = o.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   864
                passedGo = false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   865
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   866
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   867
            for (; probes > 0; probes--) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   868
                if (p == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   869
                    if (passedGo)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   870
                        break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   871
                    o = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   872
                    p = head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   873
                    passedGo = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   874
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   875
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   876
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   877
                if (it == null || it.isDetached()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   878
                    // found a discarded/exhausted iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   879
                    probes = LONG_SWEEP_PROBES; // "try harder"
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   880
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   881
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   882
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   883
                    if (o == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   884
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   885
                        if (next == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   886
                            // We've run out of iterators to track; retire
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   887
                            itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   888
                            return;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   889
                        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   890
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   891
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   892
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   893
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   894
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   895
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   896
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   897
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   898
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   899
            this.sweeper = (p == null) ? null : o;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   900
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   901
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   902
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   903
         * Adds a new iterator to the linked list of tracked iterators.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   904
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   905
        void register(Itr itr) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   906
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   907
            head = new Node(itr, head);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   908
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   909
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   910
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   911
         * Called whenever takeIndex wraps around to 0.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   912
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   913
         * Notifies all iterators, and expunges any that are now stale.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   914
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   915
        void takeIndexWrapped() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   916
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   917
            cycles++;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   918
            for (Node o = null, p = head; p != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   919
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   920
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   921
                if (it == null || it.takeIndexWrapped()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   922
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   923
                    // assert it == null || it.isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   924
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   925
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   926
                    if (o == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   927
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   928
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   929
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   930
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   931
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   932
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   933
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   934
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   935
            if (head == null)   // no more iterators to track
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   936
                itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   937
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   938
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   939
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19428
diff changeset
   940
         * Called whenever an interior remove (not at takeIndex) occurred.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   941
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   942
         * Notifies all iterators, and expunges any that are now stale.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   943
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   944
        void removedAt(int removedIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   945
            for (Node o = null, p = head; p != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   946
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   947
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   948
                if (it == null || it.removedAt(removedIndex)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   949
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   950
                    // assert it == null || it.isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   951
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   952
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   953
                    if (o == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   954
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   955
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   956
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   957
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   958
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   959
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   960
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   961
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   962
            if (head == null)   // no more iterators to track
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   963
                itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   964
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   965
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   966
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   967
         * Called whenever the queue becomes empty.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   968
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   969
         * Notifies all active iterators that the queue is empty,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   970
         * clears all weak refs, and unlinks the itrs datastructure.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   971
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   972
        void queueIsEmpty() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   973
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   974
            for (Node p = head; p != null; p = p.next) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   975
                Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   976
                if (it != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   977
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   978
                    it.shutdown();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   979
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   980
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   981
            head = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   982
            itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   983
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   984
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   985
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   986
         * Called whenever an element has been dequeued (at takeIndex).
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   987
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   988
        void elementDequeued() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   989
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   990
            if (count == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   991
                queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   992
            else if (takeIndex == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   993
                takeIndexWrapped();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   994
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   995
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   996
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   997
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   998
     * Iterator for ArrayBlockingQueue.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   999
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1000
     * To maintain weak consistency with respect to puts and takes, we
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1001
     * read ahead one slot, so as to not report hasNext true but then
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1002
     * not have an element to return.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1003
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1004
     * We switch into "detached" mode (allowing prompt unlinking from
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1005
     * itrs without help from the GC) when all indices are negative, or
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1006
     * when hasNext returns false for the first time.  This allows the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1007
     * iterator to track concurrent updates completely accurately,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1008
     * except for the corner case of the user calling Iterator.remove()
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1009
     * after hasNext() returned false.  Even in this case, we ensure
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1010
     * that we don't remove the wrong element by keeping track of the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1011
     * expected element to remove, in lastItem.  Yes, we may fail to
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1012
     * remove lastItem from the queue if it moved due to an interleaved
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1013
     * interior remove while in detached mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    private class Itr implements Iterator<E> {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1016
        /** Index to look for new nextItem; NONE at end */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1017
        private int cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1018
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1019
        /** Element to be returned by next call to next(); null if none */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1020
        private E nextItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1021
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1022
        /** Index of nextItem; NONE if none, REMOVED if removed elsewhere */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1023
        private int nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1024
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1025
        /** Last element returned; null if none or not detached. */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1026
        private E lastItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1027
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1028
        /** Index of lastItem, NONE if none, REMOVED if removed elsewhere */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1029
        private int lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1030
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1031
        /** Previous value of takeIndex, or DETACHED when detached */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1032
        private int prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1033
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1034
        /** Previous value of iters.cycles */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1035
        private int prevCycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1036
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1037
        /** Special index value indicating "not available" or "undefined" */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1038
        private static final int NONE = -1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1039
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1040
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1041
         * Special index value indicating "removed elsewhere", that is,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1042
         * removed by some operation other than a call to this.remove().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1043
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1044
        private static final int REMOVED = -2;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1045
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1046
        /** Special value for prevTakeIndex indicating "detached mode" */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1047
        private static final int DETACHED = -3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        Itr() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1050
            lastRet = NONE;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1051
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1052
            lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1053
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1054
                if (count == 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1055
                    // assert itrs == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1056
                    cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1057
                    nextIndex = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1058
                    prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1059
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1060
                    final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1061
                    prevTakeIndex = takeIndex;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1062
                    nextItem = itemAt(nextIndex = takeIndex);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1063
                    cursor = incCursor(takeIndex);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1064
                    if (itrs == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1065
                        itrs = new Itrs(this);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1066
                    } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1067
                        itrs.register(this); // in this order
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1068
                        itrs.doSomeSweeping(false);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1069
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1070
                    prevCycles = itrs.cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1071
                    // assert takeIndex >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1072
                    // assert prevTakeIndex == takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1073
                    // assert nextIndex >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1074
                    // assert nextItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1075
                }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1076
            } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1077
                lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1081
        boolean isDetached() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1082
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1083
            return prevTakeIndex < 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1084
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1085
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1086
        private int incCursor(int index) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1087
            // assert lock.isHeldByCurrentThread();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1088
            if (++index == items.length) index = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1089
            if (index == putIndex) index = NONE;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1090
            return index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1091
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1092
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1093
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1094
         * Returns true if index is invalidated by the given number of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1095
         * dequeues, starting from prevTakeIndex.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1096
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1097
        private boolean invalidated(int index, int prevTakeIndex,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1098
                                    long dequeues, int length) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1099
            if (index < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1100
                return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1101
            int distance = index - prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1102
            if (distance < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1103
                distance += length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1104
            return dequeues > distance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1107
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1108
         * Adjusts indices to incorporate all dequeues since the last
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1109
         * operation on this iterator.  Call only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1110
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1111
        private void incorporateDequeues() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1112
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1113
            // assert itrs != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1114
            // assert !isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1115
            // assert count > 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1116
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1117
            final int cycles = itrs.cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1118
            final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1119
            final int prevCycles = this.prevCycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1120
            final int prevTakeIndex = this.prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1121
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1122
            if (cycles != prevCycles || takeIndex != prevTakeIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1123
                final int len = items.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1124
                // how far takeIndex has advanced since the previous
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1125
                // operation of this iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1126
                long dequeues = (cycles - prevCycles) * len
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1127
                    + (takeIndex - prevTakeIndex);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1128
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1129
                // Check indices for invalidation
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1130
                if (invalidated(lastRet, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1131
                    lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1132
                if (invalidated(nextIndex, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1133
                    nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1134
                if (invalidated(cursor, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1135
                    cursor = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1136
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1137
                if (cursor < 0 && nextIndex < 0 && lastRet < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1138
                    detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1139
                else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1140
                    this.prevCycles = cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1141
                    this.prevTakeIndex = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1142
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1143
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1144
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1145
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1146
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1147
         * Called when itrs should stop tracking this iterator, either
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1148
         * because there are no more indices to update (cursor < 0 &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1149
         * nextIndex < 0 && lastRet < 0) or as a special exception, when
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1150
         * lastRet >= 0, because hasNext() is about to return false for the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1151
         * first time.  Call only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1152
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1153
        private void detach() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1154
            // Switch to detached mode
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1155
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1156
            // assert cursor == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1157
            // assert nextIndex < 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1158
            // assert lastRet < 0 || nextItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1159
            // assert lastRet < 0 ^ lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1160
            if (prevTakeIndex >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1161
                // assert itrs != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1162
                prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1163
                // try to unlink from itrs (but not too hard)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1164
                itrs.doSomeSweeping(true);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1165
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1166
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1167
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1168
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1169
         * For performance reasons, we would like not to acquire a lock in
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1170
         * hasNext in the common case.  To allow for this, we only access
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1171
         * fields (i.e. nextItem) that are not modified by update operations
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1172
         * triggered by queue modifications.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1173
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1174
        public boolean hasNext() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1175
            if (nextItem != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1176
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1177
            noNext();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1178
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1179
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1180
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1181
        private void noNext() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1185
                // assert cursor == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1186
                // assert nextIndex == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1187
                if (!isDetached()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1188
                    // assert lastRet >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1189
                    incorporateDequeues(); // might update lastRet
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1190
                    if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1191
                        lastItem = itemAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1192
                        // assert lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1193
                        detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1194
                    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1195
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1196
                // assert isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1197
                // assert lastRet < 0 ^ lastItem != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1203
        public E next() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1204
            final E e = nextItem;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1205
            if (e == null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1206
                throw new NoSuchElementException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1210
                if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1211
                    incorporateDequeues();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1212
                // assert nextIndex != NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1213
                // assert lastItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1214
                lastRet = nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1215
                final int cursor = this.cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1216
                if (cursor >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1217
                    nextItem = itemAt(nextIndex = cursor);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1218
                    // assert nextItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1219
                    this.cursor = incCursor(cursor);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1220
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1221
                    nextIndex = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1222
                    nextItem = null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1223
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1227
            return e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1228
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1229
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1230
        public void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1231
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1232
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1233
            lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1234
            try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1235
                final E e = nextItem;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1236
                if (e == null) return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1237
                if (!isDetached())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1238
                    incorporateDequeues();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1239
                action.accept(e);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1240
                if (isDetached() || cursor < 0) return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1241
                final Object[] items = ArrayBlockingQueue.this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1242
                for (int i = cursor, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1243
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1244
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1245
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1246
                        action.accept(itemAt(items, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1247
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1248
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1249
            } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1250
                // Calling forEachRemaining is a strong hint that this
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1251
                // iteration is surely over; supporting remove() after
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1252
                // forEachRemaining() is more trouble than it's worth
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1253
                cursor = nextIndex = lastRet = NONE;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1254
                nextItem = lastItem = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1255
                detach();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1256
                lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1257
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1259
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1260
        public void remove() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1261
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1262
            lock.lock();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1263
            // assert lock.getHoldCount() == 1;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1264
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1265
                if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1266
                    incorporateDequeues(); // might update lastRet or detach
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1267
                final int lastRet = this.lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1268
                this.lastRet = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1269
                if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1270
                    if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1271
                        removeAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1272
                    else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1273
                        final E lastItem = this.lastItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1274
                        // assert lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1275
                        this.lastItem = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1276
                        if (itemAt(lastRet) == lastItem)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1277
                            removeAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1278
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1279
                } else if (lastRet == NONE)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1280
                    throw new IllegalStateException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1281
                // else lastRet == REMOVED and the last returned element was
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1282
                // previously asynchronously removed via an operation other
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1283
                // than this.remove(), so nothing to do.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1284
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1285
                if (cursor < 0 && nextIndex < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1286
                    detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1287
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1288
                lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1289
                // assert lastRet == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1290
                // assert lastItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1291
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1292
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1293
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1294
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1295
         * Called to notify the iterator that the queue is empty, or that it
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1296
         * has fallen hopelessly behind, so that it should abandon any
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1297
         * further iteration, except possibly to return one more element
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1298
         * from next(), as promised by returning true from hasNext().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1299
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1300
        void shutdown() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1301
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1302
            cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1303
            if (nextIndex >= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1304
                nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1305
            if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1306
                lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1307
                lastItem = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1308
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1309
            prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1310
            // Don't set nextItem to null because we must continue to be
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1311
            // able to return it on next().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1312
            //
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1313
            // Caller will unlink from itrs when convenient.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1314
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1315
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1316
        private int distance(int index, int prevTakeIndex, int length) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1317
            int distance = index - prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1318
            if (distance < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1319
                distance += length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1320
            return distance;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1321
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1322
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1323
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19428
diff changeset
  1324
         * Called whenever an interior remove (not at takeIndex) occurred.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1325
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1326
         * @return true if this iterator should be unlinked from itrs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1327
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1328
        boolean removedAt(int removedIndex) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1329
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1330
            if (isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1331
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1332
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1333
            final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1334
            final int prevTakeIndex = this.prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1335
            final int len = items.length;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1336
            // distance from prevTakeIndex to removedIndex
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1337
            final int removedDistance =
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1338
                len * (itrs.cycles - this.prevCycles
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1339
                       + ((removedIndex < takeIndex) ? 1 : 0))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1340
                + (removedIndex - prevTakeIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1341
            // assert itrs.cycles - this.prevCycles >= 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1342
            // assert itrs.cycles - this.prevCycles <= 1;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1343
            // assert removedDistance > 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1344
            // assert removedIndex != takeIndex;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1345
            int cursor = this.cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1346
            if (cursor >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1347
                int x = distance(cursor, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1348
                if (x == removedDistance) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1349
                    if (cursor == putIndex)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1350
                        this.cursor = cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1351
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1352
                else if (x > removedDistance) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1353
                    // assert cursor != prevTakeIndex;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1354
                    this.cursor = cursor = dec(cursor, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1355
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1356
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1357
            int lastRet = this.lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1358
            if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1359
                int x = distance(lastRet, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1360
                if (x == removedDistance)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1361
                    this.lastRet = lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1362
                else if (x > removedDistance)
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1363
                    this.lastRet = lastRet = dec(lastRet, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1364
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1365
            int nextIndex = this.nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1366
            if (nextIndex >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1367
                int x = distance(nextIndex, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1368
                if (x == removedDistance)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1369
                    this.nextIndex = nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1370
                else if (x > removedDistance)
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1371
                    this.nextIndex = nextIndex = dec(nextIndex, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1372
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1373
            if (cursor < 0 && nextIndex < 0 && lastRet < 0) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1374
                this.prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1375
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1376
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1377
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1378
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1379
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1380
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1381
         * Called whenever takeIndex wraps around to zero.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1382
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1383
         * @return true if this iterator should be unlinked from itrs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1384
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1385
        boolean takeIndexWrapped() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1386
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1387
            if (isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1388
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1389
            if (itrs.cycles - prevCycles > 1) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1390
                // All the elements that existed at the time of the last
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1391
                // operation are gone, so abandon further iteration.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1392
                shutdown();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1393
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1394
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1395
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1396
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1397
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1398
//         /** Uncomment for debugging. */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1399
//         public String toString() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1400
//             return ("cursor=" + cursor + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1401
//                     "nextIndex=" + nextIndex + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1402
//                     "lastRet=" + lastRet + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1403
//                     "nextItem=" + nextItem + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1404
//                     "lastItem=" + lastItem + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1405
//                     "prevCycles=" + prevCycles + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1406
//                     "prevTakeIndex=" + prevTakeIndex + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1407
//                     "size()=" + size() + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1408
//                     "remainingCapacity()=" + remainingCapacity());
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1409
//         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1411
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1412
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1413
     * Returns a {@link Spliterator} over the elements in this queue.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1414
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1415
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1416
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1417
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1418
     * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1419
     * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1420
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1421
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1422
     * The {@code Spliterator} implements {@code trySplit} to permit limited
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1423
     * parallelism.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1424
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1425
     * @return a {@code Spliterator} over the elements in this queue
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1426
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1427
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1428
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1429
        return Spliterators.spliterator
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1430
            (this, (Spliterator.ORDERED |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1431
                    Spliterator.NONNULL |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1432
                    Spliterator.CONCURRENT));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1433
    }
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1434
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1435
    public void forEach(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1436
        Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1437
        final ReentrantLock lock = this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1438
        lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1439
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1440
            if (count > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1441
                final Object[] items = this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1442
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1443
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1444
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1445
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1446
                        action.accept(itemAt(items, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1447
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1448
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1449
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1450
        } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1451
            lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1452
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1453
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1454
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1455
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1456
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1457
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1458
    public boolean removeIf(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1459
        Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1460
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1461
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1462
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1463
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1464
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1465
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1466
    public boolean removeAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1467
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1468
        return bulkRemove(e -> c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1469
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1470
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1471
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1472
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1473
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1474
    public boolean retainAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1475
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1476
        return bulkRemove(e -> !c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1477
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1478
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1479
    /** Implementation of bulk remove methods. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1480
    private boolean bulkRemove(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1481
        final ReentrantLock lock = this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1482
        lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1483
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1484
            if (itrs == null) { // check for active iterators
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1485
                if (count > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1486
                    final Object[] items = this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1487
                    // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1488
                    for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1489
                             to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1490
                         ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1491
                        for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1492
                            if (filter.test(itemAt(items, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1493
                                return bulkRemoveModified(filter, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1494
                        if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1495
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1496
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1497
                return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1498
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1499
        } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1500
            lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1501
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1502
        // Active iterators are too hairy!
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1503
        // Punting (for now) to the slow n^2 algorithm ...
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1504
        return super.removeIf(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1505
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1506
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1507
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1508
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1509
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1510
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1511
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1512
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1513
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1514
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1515
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1516
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1517
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1518
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1519
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1520
     * Returns circular distance from i to j, disambiguating i == j to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1521
     * items.length; never returns 0.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1522
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1523
    private int distanceNonEmpty(int i, int j) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1524
        if ((j -= i) <= 0) j += items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1525
        return j;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1526
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1527
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1528
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1529
     * Helper for bulkRemove, in case of at least one deletion.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1530
     * Tolerate predicates that reentrantly access the collection for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1531
     * read (but not write), so traverse once to find elements to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1532
     * delete, a second pass to physically expunge.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1533
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1534
     * @param beg valid index of first element to be deleted
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1535
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1536
    private boolean bulkRemoveModified(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1537
        Predicate<? super E> filter, final int beg) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1538
        final Object[] es = items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1539
        final int capacity = items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1540
        final int end = putIndex;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1541
        final long[] deathRow = nBits(distanceNonEmpty(beg, putIndex));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1542
        deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1543
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1544
             ; i = 0, to = end, k -= capacity) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1545
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1546
                if (filter.test(itemAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1547
                    setBit(deathRow, i - k);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1548
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1549
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1550
        // a two-finger traversal, with hare i reading, tortoise w writing
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1551
        int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1552
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1553
             ; w = 0) { // w rejoins i on second leg
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1554
            // In this loop, i and w are on the same leg, with i > w
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1555
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1556
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1557
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1558
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1559
            // In this loop, w is on the first leg, i on the second
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1560
            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1561
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1562
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1563
            if (i >= to) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1564
                if (w == capacity) w = 0; // "corner" case
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1565
                break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1566
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1567
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1568
        count -= distanceNonEmpty(w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1569
        circularClear(es, putIndex = w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1570
        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1571
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1572
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1573
    /** debugging */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1574
    void checkInvariants() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1575
        // meta-assertions
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1576
        // assert lock.isHeldByCurrentThread();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1577
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1578
            // Unlike ArrayDeque, we have a count field but no spare slot.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1579
            // We prefer ArrayDeque's strategy (and the names of its fields!),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1580
            // but our field layout is baked into the serial form, and so is
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1581
            // too annoying to change.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1582
            //
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1583
            // putIndex == takeIndex must be disambiguated by checking count.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1584
            int capacity = items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1585
            // assert capacity > 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1586
            // assert takeIndex >= 0 && takeIndex < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1587
            // assert putIndex >= 0 && putIndex < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1588
            // assert count <= capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1589
            // assert takeIndex == putIndex || items[takeIndex] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1590
            // assert count == capacity || items[putIndex] == null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1591
            // assert takeIndex == putIndex || items[dec(putIndex, capacity)] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1592
        } catch (Throwable t) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1593
            System.err.printf("takeIndex=%d putIndex=%d count=%d capacity=%d%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1594
                              takeIndex, putIndex, count, items.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1595
            System.err.printf("items=%s%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1596
                              Arrays.toString(items));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1597
            throw t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1598
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1599
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1600
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
}