jdk/src/share/classes/java/lang/ref/SoftReference.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2442 f8bb207f9458
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
     2
 * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Soft reference objects, which are cleared at the discretion of the garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * collector in response to memory demand.  Soft references are most often used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * to implement memory-sensitive caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> Suppose that the garbage collector determines at a certain point in time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that an object is <a href="package-summary.html#reachability">softly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * reachable</a>.  At that time it may choose to clear atomically all soft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * references to that object and all soft references to any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * softly-reachable objects from which that object is reachable through a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of strong references.  At the same time or at some later time it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * enqueue those newly-cleared soft references that are registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * reference queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> All soft references to softly-reachable objects are guaranteed to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * been cleared before the virtual machine throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>OutOfMemoryError</code>.  Otherwise no constraints are placed upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * time at which a soft reference will be cleared or the order in which a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of such references to different objects will be cleared.  Virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * implementations are, however, encouraged to bias against clearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * recently-created or recently-used soft references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> Direct instances of this class may be used to implement simple caches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * this class or derived subclasses may also be used in larger data structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * to implement more sophisticated caches.  As long as the referent of a soft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * reference is strongly reachable, that is, is actually in use, the soft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * reference will not be cleared.  Thus a sophisticated cache can, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * prevent its most recently used entries from being discarded by keeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * strong referents to those entries, leaving the remaining entries to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * discarded at the discretion of the garbage collector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class SoftReference<T> extends Reference<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    66
    /**
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    67
     * Timestamp clock, updated by the garbage collector
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static private long clock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    71
    /**
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
    72
     * Timestamp updated by each invocation of the get method.  The VM may use
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * this field when selecting soft references to be cleared, but it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * required to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private long timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a new soft reference that refers to the given object.  The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * reference is not registered with any queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param referent object the new soft reference will refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public SoftReference(T referent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        super(referent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.timestamp = clock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates a new soft reference that refers to the given object and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * registered with the given queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param referent object the new soft reference will refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param q the queue with which the reference is to be registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          or <tt>null</tt> if registration is not required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public SoftReference(T referent, ReferenceQueue<? super T> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super(referent, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.timestamp = clock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns this reference object's referent.  If this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * been cleared, either by the program or by the garbage collector, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * this method returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return   The object to which this reference refers, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *           <code>null</code> if this reference object has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public T get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        T o = super.get();
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
   113
        if (o != null && this.timestamp != clock)
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
   114
            this.timestamp = clock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}