Java by API/java.lang.ref/WeakReference
Содержание
new WeakHashMap()
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
public class Main {
public static void main(String[] argv) throws Exception {
Object keyObject = "";
Object valueObject = "";
Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();
weakMap.put(keyObject, valueObject);
WeakReference weakValue = new WeakReference<Object>(valueObject);
weakMap.put(keyObject, weakValue);
Iterator it = weakMap.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
weakValue = (WeakReference) weakMap.get(key);
if (weakValue == null) {
System.out.println("Value has been garbage-collected");
} else {
System.out.println("Get value");
valueObject = weakValue.get();
}
}
}
}
new WeakReference(Object referent)
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
public class Main {
public static void main(String[] argv) throws Exception {
Object keyObject = "";
Object valueObject = "";
Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();
weakMap.put(keyObject, valueObject);
WeakReference weakValue = new WeakReference<Object>(valueObject);
weakMap.put(keyObject, weakValue);
Iterator it = weakMap.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
weakValue = (WeakReference) weakMap.get(key);
if (weakValue == null) {
System.out.println("Value has been garbage-collected");
} else {
System.out.println("Get value");
valueObject = weakValue.get();
}
}
}
}
new WeakReference(String referent, ReferenceQueue<? super String> q)
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
public class Main {
public static void main(String[] argv) throws Exception {
ReferenceQueue rq = new ReferenceQueue();
WeakReference<String> wr = new WeakReference<String>("string", rq);
while (true) {
Reference r = rq.remove();
if (r == wr) {
System.out.println("no longer referenced");
}
}
}
}
WeakReference: get()
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
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 WeakReference.");
reference = new WeakReference(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());
}
}
WeakReference: isEnqueued()
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
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 WeakReference.");
reference = new WeakReference(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());
}
}