0N/A/*
2362N/A * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.ref;
0N/A
0N/A
0N/A/**
0N/A * Soft reference objects, which are cleared at the discretion of the garbage
0N/A * collector in response to memory demand. Soft references are most often used
0N/A * to implement memory-sensitive caches.
0N/A *
0N/A * <p> Suppose that the garbage collector determines at a certain point in time
0N/A * that an object is <a href="package-summary.html#reachability">softly
0N/A * reachable</a>. At that time it may choose to clear atomically all soft
0N/A * references to that object and all soft references to any other
0N/A * softly-reachable objects from which that object is reachable through a chain
0N/A * of strong references. At the same time or at some later time it will
0N/A * enqueue those newly-cleared soft references that are registered with
0N/A * reference queues.
0N/A *
0N/A * <p> All soft references to softly-reachable objects are guaranteed to have
0N/A * been cleared before the virtual machine throws an
0N/A * <code>OutOfMemoryError</code>. Otherwise no constraints are placed upon the
0N/A * time at which a soft reference will be cleared or the order in which a set
0N/A * of such references to different objects will be cleared. Virtual machine
0N/A * implementations are, however, encouraged to bias against clearing
0N/A * recently-created or recently-used soft references.
0N/A *
0N/A * <p> Direct instances of this class may be used to implement simple caches;
0N/A * this class or derived subclasses may also be used in larger data structures
0N/A * to implement more sophisticated caches. As long as the referent of a soft
0N/A * reference is strongly reachable, that is, is actually in use, the soft
0N/A * reference will not be cleared. Thus a sophisticated cache can, for example,
0N/A * prevent its most recently used entries from being discarded by keeping
0N/A * strong referents to those entries, leaving the remaining entries to be
0N/A * discarded at the discretion of the garbage collector.
0N/A *
0N/A * @author Mark Reinhold
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class SoftReference<T> extends Reference<T> {
0N/A
1036N/A /**
1036N/A * Timestamp clock, updated by the garbage collector
0N/A */
0N/A static private long clock;
0N/A
1036N/A /**
1036N/A * Timestamp updated by each invocation of the get method. The VM may use
0N/A * this field when selecting soft references to be cleared, but it is not
0N/A * required to do so.
0N/A */
0N/A private long timestamp;
0N/A
0N/A /**
0N/A * Creates a new soft reference that refers to the given object. The new
0N/A * reference is not registered with any queue.
0N/A *
0N/A * @param referent object the new soft reference will refer to
0N/A */
0N/A public SoftReference(T referent) {
0N/A super(referent);
0N/A this.timestamp = clock;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new soft reference that refers to the given object and is
0N/A * registered with the given queue.
0N/A *
0N/A * @param referent object the new soft reference will refer to
0N/A * @param q the queue with which the reference is to be registered,
0N/A * or <tt>null</tt> if registration is not required
0N/A *
0N/A */
0N/A public SoftReference(T referent, ReferenceQueue<? super T> q) {
0N/A super(referent, q);
0N/A this.timestamp = clock;
0N/A }
0N/A
0N/A /**
0N/A * Returns this reference object's referent. If this reference object has
0N/A * been cleared, either by the program or by the garbage collector, then
0N/A * this method returns <code>null</code>.
0N/A *
0N/A * @return The object to which this reference refers, or
0N/A * <code>null</code> if this reference object has been cleared
0N/A */
0N/A public T get() {
0N/A T o = super.get();
1036N/A if (o != null && this.timestamp != clock)
1036N/A this.timestamp = clock;
0N/A return o;
0N/A }
0N/A
0N/A}