jdk/src/java.base/share/classes/java/util/concurrent/DelayQueue.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 32991 b27c76b82713
child 43522 f9c6f543c4db
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
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: 60
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: 60
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: 60
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
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: 7518
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
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    38
import static java.util.concurrent.TimeUnit.NANOSECONDS;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    39
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    40
import java.util.AbstractQueue;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    41
import java.util.Collection;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    42
import java.util.Iterator;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    43
import java.util.NoSuchElementException;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    44
import java.util.PriorityQueue;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    45
import java.util.concurrent.locks.Condition;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    46
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * An unbounded {@linkplain BlockingQueue blocking queue} of
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    50
 * {@code Delayed} elements, in which an element can only be taken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * when its delay has expired.  The <em>head</em> of the queue is that
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    52
 * {@code Delayed} element whose delay expired furthest in the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    53
 * past.  If no delay has expired there is no head and {@code poll}
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    54
 * will return {@code null}. Expiration occurs when an element's
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    55
 * {@code getDelay(TimeUnit.NANOSECONDS)} method returns a value less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * than or equal to zero.  Even though unexpired elements cannot be
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    57
 * removed using {@code take} or {@code poll}, they are otherwise
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    58
 * treated as normal elements. For example, the {@code size} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * returns the count of both expired and unexpired elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * This queue does not permit null elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <em>optional</em> methods of the {@link Collection} and {@link
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    64
 * Iterator} interfaces.  The Iterator provided in method {@link
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    65
 * #iterator()} is <em>not</em> guaranteed to traverse the elements of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    66
 * the DelayQueue in any particular order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    74
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    implements BlockingQueue<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    79
    private final transient ReentrantLock lock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final PriorityQueue<E> q = new PriorityQueue<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    83
     * Thread designated to wait for the element at the head of
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    84
     * the queue.  This variant of the Leader-Follower pattern
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    85
     * (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    86
     * minimize unnecessary timed waiting.  When a thread becomes
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    87
     * the leader, it waits only for the next delay to elapse, but
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    88
     * other threads await indefinitely.  The leader thread must
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    89
     * signal some other thread before returning from take() or
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    90
     * poll(...), unless some other thread becomes leader in the
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    91
     * interim.  Whenever the head of the queue is replaced with
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    92
     * an element with an earlier expiration time, the leader
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    93
     * field is invalidated by being reset to null, and some
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    94
     * waiting thread, but not necessarily the current leader, is
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    95
     * signalled.  So waiting threads must be prepared to acquire
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    96
     * and lose leadership while waiting.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    97
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    98
    private Thread leader;
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
    99
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   100
    /**
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   101
     * Condition signalled when a newer element becomes available
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   102
     * at the head of the queue or a new thread may need to
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   103
     * become leader.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   104
     */
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   105
    private final Condition available = lock.newCondition();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   106
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   107
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   108
     * Creates a new {@code DelayQueue} that is initially empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public DelayQueue() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   113
     * Creates a {@code DelayQueue} initially containing the elements of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * given collection of {@link Delayed} instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public DelayQueue(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Inserts the specified element into this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param e the element to add
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   128
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Inserts the specified element into this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param e the element to add
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   139
     * @return {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            q.offer(e);
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   147
            if (q.peek() == e) {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   148
                leader = null;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   149
                available.signal();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   150
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Inserts the specified element into this delay queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void put(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Inserts the specified element into this delay queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param timeout This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param unit This parameter is ignored as the method never blocks
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   175
     * @return {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public boolean offer(E e, long timeout, TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   183
     * Retrieves and removes the head of this queue, or returns {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * if this queue has no elements with an expired delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   186
     * @return the head of this queue, or {@code null} if this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         queue has no elements with an expired delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            E first = q.peek();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   194
            return (first == null || first.getDelay(NANOSECONDS) > 0)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   195
                ? null
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   196
                : q.poll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Retrieves and removes the head of this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * until an element with an expired delay is available on this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return the head of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                E first = q.peek();
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   215
                if (first == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    available.await();
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   217
                else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   218
                    long delay = first.getDelay(NANOSECONDS);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   219
                    if (delay <= 0L)
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   220
                        return q.poll();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   221
                    first = null; // don't retain ref while waiting
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   222
                    if (leader != null)
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   223
                        available.await();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   224
                    else {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   225
                        Thread thisThread = Thread.currentThread();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   226
                        leader = thisThread;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   227
                        try {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   228
                            available.awaitNanos(delay);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   229
                        } finally {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   230
                            if (leader == thisThread)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   231
                                leader = null;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   232
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } finally {
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   237
            if (leader == null && q.peek() != null)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   238
                available.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Retrieves and removes the head of this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * until an element with an expired delay is available on this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * or the specified wait time expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   248
     * @return the head of this queue, or {@code null} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *         specified waiting time elapses before an element with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         an expired delay becomes available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (first == null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   261
                    if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        nanos = available.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                } else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   266
                    long delay = first.getDelay(NANOSECONDS);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   267
                    if (delay <= 0L)
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   268
                        return q.poll();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   269
                    if (nanos <= 0L)
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   270
                        return null;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   271
                    first = null; // don't retain ref while waiting
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   272
                    if (nanos < delay || leader != null)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   273
                        nanos = available.awaitNanos(nanos);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   274
                    else {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   275
                        Thread thisThread = Thread.currentThread();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   276
                        leader = thisThread;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   277
                        try {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   278
                            long timeLeft = available.awaitNanos(delay);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   279
                            nanos -= delay - timeLeft;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   280
                        } finally {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   281
                            if (leader == thisThread)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   282
                                leader = null;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   283
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } finally {
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   288
            if (leader == null && q.peek() != null)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   289
                available.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Retrieves, but does not remove, the head of this queue, or
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   296
     * returns {@code null} if this queue is empty.  Unlike
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   297
     * {@code poll}, if no expired elements are available in the queue,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * this method returns the element that will expire next,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   301
     * @return the head of this queue, or {@code null} if this
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   302
     *         queue is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return q.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   325
     * Returns first element only if it is expired.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   326
     * Used only by drainTo.  Call only when holding lock.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   327
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   328
    private E peekExpired() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   329
        // assert lock.isHeldByCurrentThread();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   330
        E first = q.peek();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   331
        return (first == null || first.getDelay(NANOSECONDS) > 0) ?
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   332
            null : first;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   333
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   334
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   335
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public int drainTo(Collection<? super E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int n = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   350
            for (E e; (e = peekExpired()) != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   351
                c.add(e);       // In this order, in case add() throws.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   352
                q.poll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            int n = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   378
            for (E e; n < maxElements && (e = peekExpired()) != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   379
                c.add(e);       // In this order, in case add() throws.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   380
                q.poll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Atomically removes all of the elements from this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Elements with an unexpired delay are not waited for; they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * simply discarded from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   406
     * Always returns {@code Integer.MAX_VALUE} because
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   407
     * a {@code DelayQueue} is not capacity constrained.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   409
     * @return {@code Integer.MAX_VALUE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns an array containing all of the elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return q.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Returns an array containing all of the elements in this queue; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * runtime type of the returned array is that of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * If the queue fits in the specified array, it is returned therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Otherwise, a new array is allocated with the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * the array immediately following the end of the queue is set to
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   449
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>The following code can be used to dump a delay queue into a newly
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   457
     * allocated array of {@code Delayed}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   459
     * <pre> {@code Delayed[] a = q.toArray(new Delayed[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   461
     * Note that {@code toArray(new Object[0])} is identical in function to
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   462
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return q.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Removes a single instance of the specified element from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * queue, if it is present, whether or not it has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return q.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   498
     * Identity-based version for use in Itr.remove.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   499
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   500
    void removeEQ(Object o) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   501
        final ReentrantLock lock = this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   502
        lock.lock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   503
        try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   504
            for (Iterator<E> it = q.iterator(); it.hasNext(); ) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   505
                if (o == it.next()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   506
                    it.remove();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   507
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   508
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   509
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   510
        } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   511
            lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   512
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   513
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   514
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   515
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns an iterator over all the elements (both expired and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * unexpired) in this queue. The iterator does not return the
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   518
     * elements in any particular order.
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   519
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   520
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   521
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return an iterator over the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return new Itr(toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Snapshot iterator that works off copy of underlying q array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        final Object[] array; // Array of all elements
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   534
        int cursor;           // index of next element to return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        int lastRet;          // index of last element, or -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        Itr(Object[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return cursor < array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents: 2
diff changeset
   546
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (cursor >= array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                throw new NoSuchElementException();
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 32991
diff changeset
   550
            return (E)array[lastRet = cursor++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                throw new IllegalStateException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   556
            removeEQ(array[lastRet]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
}