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
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.
261 *
262 * <p> This method is invoked only by Java code; when the garbage collector
263 * clears references it does so directly, without invoking this method.
264 */
265 public void clear() {
266 this.referent = null;
267 }
268
269
270 /* -- Queue operations -- */
271
272 /**
273 * Tells whether or not this reference object has been enqueued, either by
|
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 import jdk.internal.HotSpotIntrinsicCandidate;
33
34 /**
35 * Abstract base class for reference objects. This class defines the
36 * operations common to all reference objects. Because reference objects are
37 * implemented in close cooperation with the garbage collector, this class may
38 * not be subclassed directly.
39 *
40 * @author Mark Reinhold
41 * @since 1.2
42 */
43
44 public abstract class Reference<T> {
45
46 /* A Reference instance is in one of four possible internal states:
47 *
48 * Active: Subject to special treatment by the garbage collector. Some
49 * time after the collector detects that the reachability of the
50 * referent has changed to the appropriate state, it changes the
51 * instance's state to either Pending or Inactive, depending upon
52 * whether or not the instance was registered with a queue when it was
235
236 // provide access in SharedSecrets
237 SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
238 @Override
239 public boolean tryHandlePendingReference() {
240 return tryHandlePending(false);
241 }
242 });
243 }
244
245 /* -- Referent accessor and setters -- */
246
247 /**
248 * Returns this reference object's referent. If this reference object has
249 * been cleared, either by the program or by the garbage collector, then
250 * this method returns <code>null</code>.
251 *
252 * @return The object to which this reference refers, or
253 * <code>null</code> if this reference object has been cleared
254 */
255 @HotSpotIntrinsicCandidate
256 public T get() {
257 return this.referent;
258 }
259
260 /**
261 * Clears this reference object. Invoking this method will not cause this
262 * object to be enqueued.
263 *
264 * <p> This method is invoked only by Java code; when the garbage collector
265 * clears references it does so directly, without invoking this method.
266 */
267 public void clear() {
268 this.referent = null;
269 }
270
271
272 /* -- Queue operations -- */
273
274 /**
275 * Tells whether or not this reference object has been enqueued, either by
|