1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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 sun.misc.Cleaner;
29 import sun.misc.JavaLangRefAccess;
30 import sun.misc.ManagedLocalsThread;
31 import sun.misc.SharedSecrets;
32
33 /**
34 * Abstract base class for reference objects. This class defines the
35 * operations common to all reference objects. Because reference objects are
36 * implemented in close cooperation with the garbage collector, this class may
37 * not be subclassed directly.
38 *
39 * @author Mark Reinhold
40 * @since 1.2
41 */
42
43 public abstract class Reference<T> {
44
45 /* A Reference instance is in one of four possible internal states:
46 *
47 * Active: Subject to special treatment by the garbage collector. Some
48 * time after the collector detects that the reachability of the
49 * referent has changed to the appropriate state, it changes the
50 * instance's state to either Pending or Inactive, depending upon
51 * whether or not the instance was registered with a queue when it was
52 * created. In the former case it also adds the instance to the
221
222 static {
223 ThreadGroup tg = Thread.currentThread().getThreadGroup();
224 for (ThreadGroup tgn = tg;
225 tgn != null;
226 tg = tgn, tgn = tg.getParent());
227 Thread handler = new ReferenceHandler(tg, "Reference Handler");
228 /* If there were a special system-only priority greater than
229 * MAX_PRIORITY, it would be used here
230 */
231 handler.setPriority(Thread.MAX_PRIORITY);
232 handler.setDaemon(true);
233 handler.start();
234
235 // provide access in SharedSecrets
236 SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
237 @Override
238 public boolean tryHandlePendingReference() {
239 return tryHandlePending(false);
240 }
241 });
242 }
243
244 /* -- Referent accessor and setters -- */
245
246 /**
247 * Returns this reference object's referent. If this reference object has
248 * been cleared, either by the program or by the garbage collector, then
249 * this method returns <code>null</code>.
250 *
251 * @return The object to which this reference refers, or
252 * <code>null</code> if this reference object has been cleared
253 */
254 public T get() {
255 return this.referent;
256 }
257
258 /**
259 * Clears this reference object. Invoking this method will not cause this
260 * object to be enqueued.
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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 sun.misc.Cleaner;
29 import sun.misc.JavaLangRefAccess;
30 import sun.misc.ManagedLocalsThread;
31 import sun.misc.SharedSecrets;
32
33 import java.util.function.Consumer;
34 import java.util.function.Supplier;
35
36 /**
37 * Abstract base class for reference objects. This class defines the
38 * operations common to all reference objects. Because reference objects are
39 * implemented in close cooperation with the garbage collector, this class may
40 * not be subclassed directly.
41 *
42 * @author Mark Reinhold
43 * @since 1.2
44 */
45
46 public abstract class Reference<T> {
47
48 /* A Reference instance is in one of four possible internal states:
49 *
50 * Active: Subject to special treatment by the garbage collector. Some
51 * time after the collector detects that the reachability of the
52 * referent has changed to the appropriate state, it changes the
53 * instance's state to either Pending or Inactive, depending upon
54 * whether or not the instance was registered with a queue when it was
55 * created. In the former case it also adds the instance to the
224
225 static {
226 ThreadGroup tg = Thread.currentThread().getThreadGroup();
227 for (ThreadGroup tgn = tg;
228 tgn != null;
229 tg = tgn, tgn = tg.getParent());
230 Thread handler = new ReferenceHandler(tg, "Reference Handler");
231 /* If there were a special system-only priority greater than
232 * MAX_PRIORITY, it would be used here
233 */
234 handler.setPriority(Thread.MAX_PRIORITY);
235 handler.setDaemon(true);
236 handler.start();
237
238 // provide access in SharedSecrets
239 SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
240 @Override
241 public boolean tryHandlePendingReference() {
242 return tryHandlePending(false);
243 }
244 @Override
245 public <T> Reference<T> createFinalizator(T finalizee, Consumer<? super T> thunk) {
246 return new Finalizator<>(finalizee, thunk);
247 }
248 });
249 }
250
251 /* -- Referent accessor and setters -- */
252
253 /**
254 * Returns this reference object's referent. If this reference object has
255 * been cleared, either by the program or by the garbage collector, then
256 * this method returns <code>null</code>.
257 *
258 * @return The object to which this reference refers, or
259 * <code>null</code> if this reference object has been cleared
260 */
261 public T get() {
262 return this.referent;
263 }
264
265 /**
266 * Clears this reference object. Invoking this method will not cause this
267 * object to be enqueued.
|