Java by API/java.lang.ref/PhantomReference — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
new PhantomReference(T referent, ReferenceQueue<?> q)
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ReferenceQueue referenceQueue = new ReferenceQueue();
Object object = new Object() {
public String toString() {
return "Referenced Object";
}
};
Object data = new Object() {
public String toString() {
return "Data";
}
};
HashMap map = new HashMap();
Reference reference = null;
System.out.println("Testing PhantomReference.");
reference = new PhantomReference(object, referenceQueue);
map.put(reference, data);
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
object = null;
data = null;
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
}
}
PhantomReference: get()
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ReferenceQueue referenceQueue = new ReferenceQueue();
Object object = new Object() {
public String toString() {
return "Referenced Object";
}
};
Object data = new Object() {
public String toString() {
return "Data";
}
};
HashMap map = new HashMap();
Reference reference = null;
System.out.println("Testing PhantomReference.");
reference = new PhantomReference(object, referenceQueue);
map.put(reference, data);
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
object = null;
data = null;
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
}
}
PhantomReference: isEnqueued()
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ReferenceQueue referenceQueue = new ReferenceQueue();
Object object = new Object() {
public String toString() {
return "Referenced Object";
}
};
Object data = new Object() {
public String toString() {
return "Data";
}
};
HashMap map = new HashMap();
Reference reference = null;
System.out.println("Testing PhantomReference.");
reference = new PhantomReference(object, referenceQueue);
map.put(reference, data);
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
object = null;
data = null;
System.gc();
System.out.println(reference.get());
System.out.println(map.get(reference));
System.out.println(reference.isEnqueued());
}
}