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 }
66 r.queue = ENQUEUED;
67 r.next = (head == null) ? r : head;
68 head = r;
69 queueLength++;
70 if (r instanceof FinalReference) {
71 sun.misc.VM.addFinalRefCount(1);
72 }
73 lock.notifyAll();
74 return true;
75 }
76 }
77
78 @SuppressWarnings("unchecked")
79 private Reference<? extends T> reallyPoll() { /* Must hold lock */
80 Reference<? extends T> r = head;
81 if (r != null) {
82 head = (r.next == r) ?
83 null :
84 r.next; // Unchecked due to the next field having a raw type in Reference
85 r.queue = NULL;
86 r.next = r;
87 queueLength--;
88 if (r instanceof FinalReference) {
89 sun.misc.VM.addFinalRefCount(-1);
90 }
91 return r;
92 }
93 return null;
94 }
95
96 /**
97 * Polls this queue to see if a reference object is available. If one is
98 * available without further delay then it is removed from the queue and
99 * returned. Otherwise this method immediately returns <tt>null</tt>.
100 *
101 * @return A reference object, if one was immediately available,
102 * otherwise <code>null</code>
103 */
104 public Reference<? extends T> poll() {
105 if (head == null)
106 return null;
147 long end = System.nanoTime();
148 timeout -= (end - start) / 1000_000;
149 if (timeout <= 0) return null;
150 start = end;
151 }
152 }
153 }
154 }
155
156 /**
157 * Removes the next reference object in this queue, blocking until one
158 * becomes available.
159 *
160 * @return A reference object, blocking until one becomes available
161 * @throws InterruptedException If the wait is interrupted
162 */
163 public Reference<? extends T> remove() throws InterruptedException {
164 return remove(0);
165 }
166
167 }
|
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 java.util.function.Consumer;
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 }
68 r.queue = ENQUEUED;
69 r.next = (head == null) ? r : head;
70 head = r;
71 queueLength++;
72 if (r instanceof FinalReference) {
73 sun.misc.VM.addFinalRefCount(1);
74 }
75 lock.notifyAll();
76 return true;
77 }
78 }
79
80 @SuppressWarnings("unchecked")
81 private Reference<? extends T> reallyPoll() { /* Must hold lock */
82 Reference<? extends T> r = head;
83 if (r != null) {
84 head = (r.next == r) ?
85 null :
86 r.next; // Unchecked due to the next field having a raw type in Reference
87 r.queue = NULL;
88 r.next = Reference.DEQUEUED;
89 queueLength--;
90 if (r instanceof FinalReference) {
91 sun.misc.VM.addFinalRefCount(-1);
92 }
93 return r;
94 }
95 return null;
96 }
97
98 /**
99 * Polls this queue to see if a reference object is available. If one is
100 * available without further delay then it is removed from the queue and
101 * returned. Otherwise this method immediately returns <tt>null</tt>.
102 *
103 * @return A reference object, if one was immediately available,
104 * otherwise <code>null</code>
105 */
106 public Reference<? extends T> poll() {
107 if (head == null)
108 return null;
149 long end = System.nanoTime();
150 timeout -= (end - start) / 1000_000;
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 /**
170 * Iterate queue and invoke given action with each Reference.
171 * Suitable for diagnostic purposes.
172 */
173 void forEach(Consumer<? super Reference<? extends T>> action) {
174 for (Reference<? extends T> r = head; r != null;) {
175 action.accept(r);
176 Reference rn = r.next;
177 if (rn == r) {
178 // end of chain
179 r = null;
180 } else if (rn == Reference.DEQUEUED) {
181 // restart from head if overtaken by queue poller(s)
182 r = head;
183 } else {
184 r = rn;
185 }
186 }
187 }
188 }
|