你好!
我对WeakReference有个疑问,请看下面的例子:
class VeryBig {
private String ident;
public VeryBig(String id) { ident = id; }
public String toString() { return ident; }
public void finalize() {
System.out.println("Finalizing " + ident );
}
}public class References1{
private static ReferenceQueue rq = new ReferenceQueue();
public static void checkQueue() {
Object inq = rq.poll();
if(inq != null)
System.out.println("In queue: " +
(VeryBig)((Reference)inq).get());
} public static void main(String[] args) {
WeakReference wa = new WeakReference(new VeryBig("Weak " + 0), rq); System.out.println("Just created: " + (VeryBig)wa.get()); System.gc(); checkQueue();
}
}执行结果:
Just created: Weak 0
Finalizing Weak 01. 回收器已经开始回收了,为什么没有执行
if(inq != null)
System.out.println("In queue: " +
(VeryBig)((Reference)inq).get());
我对WeakReference有个疑问,请看下面的例子:
class VeryBig {
private String ident;
public VeryBig(String id) { ident = id; }
public String toString() { return ident; }
public void finalize() {
System.out.println("Finalizing " + ident );
}
}public class References1{
private static ReferenceQueue rq = new ReferenceQueue();
public static void checkQueue() {
Object inq = rq.poll();
if(inq != null)
System.out.println("In queue: " +
(VeryBig)((Reference)inq).get());
} public static void main(String[] args) {
WeakReference wa = new WeakReference(new VeryBig("Weak " + 0), rq); System.out.println("Just created: " + (VeryBig)wa.get()); System.gc(); checkQueue();
}
}执行结果:
Just created: Weak 0
Finalizing Weak 01. 回收器已经开始回收了,为什么没有执行
if(inq != null)
System.out.println("In queue: " +
(VeryBig)((Reference)inq).get());
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货