jdk/src/share/classes/java/lang/ref/ReferenceQueue.java
author martin
Sun, 14 Jun 2009 14:33:30 -0700
changeset 2947 b0135c99348e
parent 2442 f8bb207f9458
child 5506 202f599c92aa
permissions -rw-r--r--
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement Summary: Allow random access to stack trace elements; retrieve only needed ones Reviewed-by: swamyv Contributed-by: jeremymanson@google.com
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
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 java.lang.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Reference queues, to which registered reference objects are appended by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * garbage collector after the appropriate reachability changes are detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since    1.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
public class ReferenceQueue<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Constructs a new reference-object queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public ReferenceQueue() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static class Null extends ReferenceQueue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        boolean enqueue(Reference r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            return false;
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
    static ReferenceQueue NULL = new Null();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static ReferenceQueue ENQUEUED = new Null();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static private class Lock { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Lock lock = new Lock();
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    54
    private volatile Reference<? extends T> head = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private long queueLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        synchronized (r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (r.queue == ENQUEUED) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                r.queue = ENQUEUED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                r.next = (head == null) ? r : head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                head = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                queueLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                if (r instanceof FinalReference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    sun.misc.VM.addFinalRefCount(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Reference<? extends T> reallyPoll() {       /* Must hold lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Reference<? extends T> r = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            head = (r.next == r) ? null : r.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            r.queue = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            r.next = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            queueLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (r instanceof FinalReference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                sun.misc.VM.addFinalRefCount(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Polls this queue to see if a reference object is available.  If one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * available without further delay then it is removed from the queue and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * returned.  Otherwise this method immediately returns <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return  A reference object, if one was immediately available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          otherwise <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public Reference<? extends T> poll() {
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    98
        if (head == null)
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    99
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
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
     * Removes the next reference object in this queue, blocking until either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * one becomes available or the given timeout period expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p> This method does not offer real-time guarantees: It schedules the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * timeout as if by invoking the {@link Object#wait(long)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param  timeout  If positive, block for up to <code>timeout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                  milliseconds while waiting for a reference to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *                  added to this queue.  If zero, block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return  A reference object, if one was available within the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          timeout period, otherwise <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          If the value of the timeout argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws  InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *          If the timeout wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public Reference<? extends T> remove(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws IllegalArgumentException, InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (timeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("Negative timeout value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            Reference<? extends T> r = reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (r != null) return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                lock.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                r = reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (r != null) return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (timeout != 0) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Removes the next reference object in this queue, blocking until one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * becomes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return A reference object, blocking until one becomes available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws  InterruptedException  If the wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public Reference<? extends T> remove() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}