001 /* java.lang.ref.ReferenceQueue 002 Copyright (C) 1999, 2004, 2006 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.lang.ref; 040 041 /** 042 * This is the queue, where references can enqueue themselve on. Each 043 * reference may be registered to a queue at initialization time and 044 * will be appended to the queue, when the enqueue method is called. 045 * 046 * The enqueue method may be automatically called by the garbage 047 * collector if it detects, that the object is only reachable through 048 * the Reference objects. 049 * 050 * @author Jochen Hoenicke 051 * @see Reference#enqueue() 052 */ 053 public class ReferenceQueue<T> 054 { 055 /** 056 * This is a linked list of references. If this is null, the list is 057 * empty. Otherwise this points to the first reference on the queue. 058 * The first reference will point to the next reference via the 059 * <code>nextOnQueue</code> field. The last reference will point to 060 * itself (not to null, since <code>nextOnQueue</code> is used to 061 * determine if a reference is enqueued). 062 */ 063 private Reference<? extends T> first; 064 065 /** 066 * This is the lock that protects our linked list and is used to signal 067 * a thread waiting in remove(). 068 */ 069 private final Object lock = new Object(); 070 071 /** 072 * Creates a new empty reference queue. 073 */ 074 public ReferenceQueue() 075 { 076 } 077 078 /** 079 * Checks if there is a reference on the queue, returning it 080 * immediately. The reference will be dequeued. 081 * 082 * @return a reference on the queue, if there is one, 083 * <code>null</code> otherwise. 084 */ 085 public Reference<? extends T> poll() 086 { 087 return dequeue(); 088 } 089 090 /** 091 * This is called by reference to enqueue itself on this queue. 092 * @param ref the reference that should be enqueued. 093 * @return true if successful, false if not. 094 */ 095 final boolean enqueue(Reference<? extends T> ref) 096 { 097 synchronized (lock) 098 { 099 if (ref.queue != this) 100 return false; 101 102 /* last reference will point to itself */ 103 ref.nextOnQueue = first == null ? ref : first; 104 ref.queue = null; 105 first = ref; 106 /* this wakes only one remove thread. */ 107 lock.notify(); 108 return true; 109 } 110 } 111 112 /** 113 * Remove a reference from the queue, if there is one. 114 * @return the first element of the queue, or null if there isn't any. 115 */ 116 private Reference<? extends T> dequeue() 117 { 118 synchronized (lock) 119 { 120 if (first == null) 121 return null; 122 123 Reference<? extends T> result = first; 124 first = (first == first.nextOnQueue) ? null : first.nextOnQueue; 125 result.nextOnQueue = null; 126 return result; 127 } 128 } 129 130 /** 131 * Removes a reference from the queue, blocking for <code>timeout</code> 132 * until a reference is enqueued. 133 * @param timeout the timeout period in milliseconds, <code>0</code> means 134 * wait forever. 135 * @return the reference removed from the queue, or 136 * <code>null</code> if timeout period expired. 137 * @exception InterruptedException if the wait was interrupted. 138 */ 139 public Reference<? extends T> remove(long timeout) 140 throws InterruptedException 141 { 142 synchronized (lock) 143 { 144 if (first == null) 145 lock.wait(timeout); 146 } 147 148 return dequeue(); 149 } 150 151 152 /** 153 * Removes a reference from the queue, blocking until a reference is 154 * enqueued. 155 * 156 * @return the reference removed from the queue. 157 * @exception InterruptedException if the wait was interrupted. 158 */ 159 public Reference<? extends T> remove() 160 throws InterruptedException 161 { 162 return remove(0L); 163 } 164 }