java类库详细讲解
源代码在线查看: weakref.html
Determining When an Object Is No Longer Used
(Java Developers Almanac Example)
BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
The Java Developers Almanac 1.4
Order this book from Amazon.
Home
>
List of Packages
>
java.lang.ref
[3 examples]
e107.
Determining When an Object Is No Longer Used
A weak reference is used to determine when an object
is no longer being referenced.
// Create the weak reference.
ReferenceQueue rq = new ReferenceQueue();
WeakReference wr = new WeakReference(object, rq);
// Wait for all the references to the object.
try {
while (true) {
Reference r = rq.remove();
if (r == wr) {
// Object is no longer referenced.
}
}
} catch (InterruptedException e) {
}
Related Examples
e106.
Holding onto an Object Until Memory Becomes Low
e108.
Determining When an Object Will Be Reclaimed
© 2002 Addison-Wesley.