8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.ref;
27
28 /**
29 * Reference queues, to which registered reference objects are appended by the
30 * garbage collector after the appropriate reachability changes are detected.
31 *
32 * @author Mark Reinhold
33 * @since 1.2
34 */
35
36 public class ReferenceQueue<T> {
37
38 /**
39 * Constructs a new reference-object queue.
40 */
41 public ReferenceQueue() { }
42
43 private static class Null<S> extends ReferenceQueue<S> {
44 boolean enqueue(Reference<? extends S> r) {
45 return false;
46 }
47 }
121 * added to this queue. If zero, block indefinitely.
122 *
123 * @return A reference object, if one was available within the specified
124 * timeout period, otherwise <code>null</code>
125 *
126 * @throws IllegalArgumentException
127 * If the value of the timeout argument is negative
128 *
129 * @throws InterruptedException
130 * If the timeout wait is interrupted
131 */
132 public Reference<? extends T> remove(long timeout)
133 throws IllegalArgumentException, InterruptedException
134 {
135 if (timeout < 0) {
136 throw new IllegalArgumentException("Negative timeout value");
137 }
138 synchronized (lock) {
139 Reference<? extends T> r = reallyPoll();
140 if (r != null) return r;
141 for (;;) {
142 lock.wait(timeout);
143 r = reallyPoll();
144 if (r != null) return r;
145 if (timeout != 0) return null;
146 }
147 }
148 }
149
150 /**
151 * Removes the next reference object in this queue, blocking until one
152 * becomes available.
153 *
154 * @return A reference object, blocking until one becomes available
155 * @throws InterruptedException If the wait is interrupted
156 */
157 public Reference<? extends T> remove() throws InterruptedException {
158 return remove(0);
159 }
160
161 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.ref;
27
28 import static java.util.concurrent.TimeUnit.*;
29
30 /**
31 * Reference queues, to which registered reference objects are appended by the
32 * garbage collector after the appropriate reachability changes are detected.
33 *
34 * @author Mark Reinhold
35 * @since 1.2
36 */
37
38 public class ReferenceQueue<T> {
39
40 /**
41 * Constructs a new reference-object queue.
42 */
43 public ReferenceQueue() { }
44
45 private static class Null<S> extends ReferenceQueue<S> {
46 boolean enqueue(Reference<? extends S> r) {
47 return false;
48 }
49 }
123 * added to this queue. If zero, block indefinitely.
124 *
125 * @return A reference object, if one was available within the specified
126 * timeout period, otherwise <code>null</code>
127 *
128 * @throws IllegalArgumentException
129 * If the value of the timeout argument is negative
130 *
131 * @throws InterruptedException
132 * If the timeout wait is interrupted
133 */
134 public Reference<? extends T> remove(long timeout)
135 throws IllegalArgumentException, InterruptedException
136 {
137 if (timeout < 0) {
138 throw new IllegalArgumentException("Negative timeout value");
139 }
140 synchronized (lock) {
141 Reference<? extends T> r = reallyPoll();
142 if (r != null) return r;
143 long start = (timeout == 0) ? 0 : System.nanoTime();
144 for (;;) {
145 lock.wait(timeout);
146 r = reallyPoll();
147 if (r != null) return r;
148 if (timeout != 0) {
149 long end = System.nanoTime();
150 timeout -= NANOSECONDS.toMillis(end - start);
151 if (timeout <= 0) return null;
152 start = end;
153 }
154 }
155 }
156 }
157
158 /**
159 * Removes the next reference object in this queue, blocking until one
160 * becomes available.
161 *
162 * @return A reference object, blocking until one becomes available
163 * @throws InterruptedException If the wait is interrupted
164 */
165 public Reference<? extends T> remove() throws InterruptedException {
166 return remove(0);
167 }
168
169 }
|