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