jdk/src/share/classes/sun/misc/Queue.java
author alanb
Fri, 22 Feb 2013 14:04:06 +0000
changeset 16023 58ecc1b8327b
parent 14342 8435a30053c1
permissions -rw-r--r--
8008290: (profiles) Startup regression due to additional checking of JAR file manifests Reviewed-by: lancea, chegar, iris, mchung, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11131
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Queue: implements a simple queue mechanism.  Allows for enumeration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    38
public class Queue<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    42
    QueueElement<T> head = null;
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    43
    QueueElement<T> tail = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public Queue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Enqueue an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    51
    public synchronized void enqueue(T obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    53
        QueueElement<T> newElt = new QueueElement<>(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            head = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            tail = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            newElt.next = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            head.prev = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            head = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            length++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Dequeue the oldest object on the queue.  Will wait indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return    the oldest object on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @exception java.lang.InterruptedException if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *              interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    75
    public T dequeue() throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return dequeue(0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Dequeue the oldest object on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param timeOut the number of milliseconds to wait for something
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * to arrive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return    the oldest object on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception java.lang.InterruptedException if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *              interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    88
    public synchronized T dequeue(long timeOut)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (tail == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            wait(timeOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
    94
        QueueElement<T> elt = tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        tail = elt.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (tail == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            tail.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        length--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return elt.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Is the queue empty?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return true if the queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public synchronized boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return (tail == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns an enumeration of the elements in Last-In, First-Out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * order. Use the Enumeration methods on the returned object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   118
    public final synchronized Enumeration<T> elements() {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   119
        return new LIFOQueueEnumerator<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns an enumeration of the elements in First-In, First-Out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * order. Use the Enumeration methods on the returned object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   127
    public final synchronized Enumeration<T> reverseElements() {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   128
        return new FIFOQueueEnumerator<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public synchronized void dump(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.err.println(">> "+msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.err.println("["+length+" elt(s); head = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                           (head == null ? "null" : (head.obj)+"")+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                           " tail = "+(tail == null ? "null" : (tail.obj)+""));
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   136
        QueueElement<T> cursor = head;
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   137
        QueueElement<T> last = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        while (cursor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.err.println("  "+cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            last = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            cursor = cursor.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (last != tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.err.println("  tail != last: "+tail+", "+last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.err.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   150
final class FIFOQueueEnumerator<T> implements Enumeration<T> {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   151
    Queue<T> queue;
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   152
    QueueElement<T> cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   154
    FIFOQueueEnumerator(Queue<T> q) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        queue = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cursor = q.tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (cursor != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   163
    public T nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        synchronized (queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (cursor != null) {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   166
                QueueElement<T> result = cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                cursor = cursor.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return result.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throw new NoSuchElementException("FIFOQueueEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   175
final class LIFOQueueEnumerator<T> implements Enumeration<T> {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   176
    Queue<T> queue;
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   177
    QueueElement<T> cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   179
    LIFOQueueEnumerator(Queue<T> q) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        queue = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        cursor = q.head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return (cursor != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   188
    public T nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        synchronized (queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (cursor != null) {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   191
                QueueElement<T> result = cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                cursor = cursor.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return result.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throw new NoSuchElementException("LIFOQueueEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   200
class QueueElement<T> {
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   201
    QueueElement<T> next = null;
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   202
    QueueElement<T> prev = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   204
    T obj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 5506
diff changeset
   206
    QueueElement(T obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return "QueueElement[obj="+obj+(prev == null ? " null" : " prev")+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            (next == null ? " null" : " next")+"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}