Java Reference
In-Depth Information
If an object with an
id
of 5 is not already in the cache, it will be cached and the new object reference will be
assigned to
cachedObject
. If an object with an
id
of 5 is already in the cache, the reference of that object from the
cache will be returned and assigned to
cachedObject
.
Using the ReferenceQueue Class
An object of the
ReferenceQueue
class is used in conjunction with objects of the
SoftReference
,
WeakReference
, and
PhantomReference
classes when the object needs to be notified upon its reachability change. An object of any of these
reference classes can be registered with a reference queue, as shown:
ReferenceQueue q = new ReferenceQueue();
SoftReference sr = new SoftReference(new BigObject(19), q);
WeakReference wr = new WeakReference(new BigObject(20), q);
PhantomReference pr = new PhantomReference(new BigObject(21), q);
It is optional to register the
SoftReference
and
WeakReference
objects with a reference queue. However, you
must register a
PhantomReference
object with a reference queue. When a
SoftReference
or
WeakReference
is cleared
by the garbage collector, the reference of the
SoftReference
or the
WeakReference
object is appended to the reference
queue. Note the references of the
SoftReference
and
WeakReference
are placed in the queue, not the reference of
their referent. For example, if the garbage collector clears the soft reference to a
BigObject
with
id
19 in the above
snippet of code,
sr
will be placed in the reference queue. In case of a
PhantomReference
, when its referent becomes
phantom reachable, the garbage collector places the
PhantomReference
object in the reference queue. Unlike soft
and weak references, the garbage collector does not clear the phantom references as it places them in their reference
queue. The program must clear the phantom references by calling the
clear()
method.
There are two ways to determine if a reference object has been placed in its reference queue. You can call the
poll()
or
remove()
method on a
ReferenceQueue
object, or you can call the
isEnqueued()
method on the soft,
weak, and phantom references. The
poll()
method removes a reference from the queue and returns it. If there is no
reference available in the queue, it returns
null
. The
remove()
method works the same as the
poll()
method, except
that if there is no reference available in the queue, it blocks until it becomes available. The
isEnqueued()
method for
soft, weak, and phantom references returns
true
if they are placed in queue. Otherwise, it returns
false
. Listing 11-8
demonstrates how to use the
ReferenceQueue
class.
Listing 11-8.
Using the ReferenceQueue Class
// ReferenceQueueDemo.java
package com.jdojo.gc;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
public class ReferenceQueueDemo {
public static void main(String[] args) {
// Create a reference queue
ReferenceQueue<BigObject> q = new ReferenceQueue<BigObject>();
// Wrap a BigObject inside a soft reference.
// Also register the soft reference with the reference queue
BigObject bigObj = new BigObject(131);
WeakReference<BigObject> wr = new WeakReference<BigObject>(bigObj, q);