Class SoftReference<T>
- Type Parameters:
T- the type of the referent
Suppose that the garbage collector determines at a certain point in timethat an object issoftlyreachable. At that time it may choose to clear atomically all softreferences to that object and all soft references to any othersoftly-reachable objects from which that object is reachable through a chainof strong references. At the same time or at some later time it willenqueue those newly-cleared soft references that are registered withreference queues.
All soft references to softly-reachable objects are guaranteed to havebeen cleared before the virtual machine throws anOutOfMemoryError. Otherwise no constraints are placed upon thetime at which a soft reference will be cleared or the order in which a setof such references to different objects will be cleared. Virtual machineimplementations are, however, encouraged to bias against clearingrecently-created or recently-used soft references.
Direct instances of this class may be used to implement simple caches;this class or derived subclasses may also be used in larger data structuresto implement more sophisticated caches. As long as the referent of a softreference is strongly reachable, that is, is actually in use, the softreference will not be cleared. Thus a sophisticated cache can, for example,prevent its most recently used entries from being discarded by keepingstrong referents to those entries, leaving the remaining entries to bediscarded at the discretion of the garbage collector.
- Since:
- 1.2
Constructor Summary
ConstructorsConstructorDescriptionSoftReference(T referent) Creates a new soft reference that refers to the given object.SoftReference(T referent,ReferenceQueue<? superT> q) Creates a new soft reference that refers to the given object and isregistered with the given queue.Method Summary
Methods declared in class Reference
clear,clone,enqueue,isEnqueued,reachabilityFence,refersTo
Constructor Details
SoftReference
Creates a new soft reference that refers to the given object. The newreference is not registered with any queue.- Parameters:
referent- object the new soft reference will refer to
SoftReference
Creates a new soft reference that refers to the given object and isregistered with the given queue.- Parameters:
referent- object the new soft reference will refer toq- the queue with which the reference is to be registered, ornullif registration is not required
Method Details
get
Returns this reference object's referent. If this reference object hasbeen cleared, either by the program or by the garbage collector, thenthis method returnsnull.