jaroslav@601: /* jaroslav@601: * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.lang.ref; jaroslav@601: jaroslav@601: jaroslav@601: /** jaroslav@601: * Soft reference objects, which are cleared at the discretion of the garbage jaroslav@601: * collector in response to memory demand. Soft references are most often used jaroslav@601: * to implement memory-sensitive caches. jaroslav@601: * jaroslav@601: *

Suppose that the garbage collector determines at a certain point in time jaroslav@601: * that an object is softly jaroslav@601: * reachable. At that time it may choose to clear atomically all soft jaroslav@601: * references to that object and all soft references to any other jaroslav@601: * softly-reachable objects from which that object is reachable through a chain jaroslav@601: * of strong references. At the same time or at some later time it will jaroslav@601: * enqueue those newly-cleared soft references that are registered with jaroslav@601: * reference queues. jaroslav@601: * jaroslav@601: *

All soft references to softly-reachable objects are guaranteed to have jaroslav@601: * been cleared before the virtual machine throws an jaroslav@601: * OutOfMemoryError. Otherwise no constraints are placed upon the jaroslav@601: * time at which a soft reference will be cleared or the order in which a set jaroslav@601: * of such references to different objects will be cleared. Virtual machine jaroslav@601: * implementations are, however, encouraged to bias against clearing jaroslav@601: * recently-created or recently-used soft references. jaroslav@601: * jaroslav@601: *

Direct instances of this class may be used to implement simple caches; jaroslav@601: * this class or derived subclasses may also be used in larger data structures jaroslav@601: * to implement more sophisticated caches. As long as the referent of a soft jaroslav@601: * reference is strongly reachable, that is, is actually in use, the soft jaroslav@601: * reference will not be cleared. Thus a sophisticated cache can, for example, jaroslav@601: * prevent its most recently used entries from being discarded by keeping jaroslav@601: * strong referents to those entries, leaving the remaining entries to be jaroslav@601: * discarded at the discretion of the garbage collector. jaroslav@601: * jaroslav@601: * @author Mark Reinhold jaroslav@601: * @since 1.2 jaroslav@601: */ jaroslav@601: jaroslav@601: public class SoftReference extends Reference { jaroslav@601: jaroslav@601: /** jaroslav@601: * Timestamp clock, updated by the garbage collector jaroslav@601: */ jaroslav@601: static private long clock; jaroslav@601: jaroslav@601: /** jaroslav@601: * Timestamp updated by each invocation of the get method. The VM may use jaroslav@601: * this field when selecting soft references to be cleared, but it is not jaroslav@601: * required to do so. jaroslav@601: */ jaroslav@601: private long timestamp; jaroslav@601: jaroslav@601: /** jaroslav@601: * Creates a new soft reference that refers to the given object. The new jaroslav@601: * reference is not registered with any queue. jaroslav@601: * jaroslav@601: * @param referent object the new soft reference will refer to jaroslav@601: */ jaroslav@601: public SoftReference(T referent) { jaroslav@601: super(referent); jaroslav@601: this.timestamp = clock; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Creates a new soft reference that refers to the given object and is jaroslav@601: * registered with the given queue. jaroslav@601: * jaroslav@601: * @param referent object the new soft reference will refer to jaroslav@601: * @param q the queue with which the reference is to be registered, jaroslav@601: * or null if registration is not required jaroslav@601: * jaroslav@601: */ jaroslav@601: public SoftReference(T referent, ReferenceQueue q) { jaroslav@601: super(referent, q); jaroslav@601: this.timestamp = clock; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns this reference object's referent. If this reference object has jaroslav@601: * been cleared, either by the program or by the garbage collector, then jaroslav@601: * this method returns null. jaroslav@601: * jaroslav@601: * @return The object to which this reference refers, or jaroslav@601: * null if this reference object has been cleared jaroslav@601: */ jaroslav@601: public T get() { jaroslav@601: T o = super.get(); jaroslav@601: if (o != null && this.timestamp != clock) jaroslav@601: this.timestamp = clock; jaroslav@601: return o; jaroslav@601: } jaroslav@601: jaroslav@601: }