我正在阅读以下有关
java中弱引用的帖子: –
Understanding Weak References.
在完成理论部分之后,尝试测试无效条件的弱引用.但是,对于弱引用的null检查永远不会在以下代码中返回true: –
package com.weak; import java.lang.ref.WeakReference; class Widget{} public class WeakReferenceDemo { public static void main(String[] args) throws InterruptedException { Widget widget = new Widget() ; WeakReference<Widget> valueWrapper = new WeakReference<Widget>(widget) ; System.out.println( valueWrapper.get() ); //here strong reference to object is lost widget = null ; int count = 0 ; //here checking for null condition while( valueWrapper.get() != null ){ System.out.print("."); Thread.sleep(432) ; if(++count % 25 == 0) System.out.println(); } System.out.println( valueWrapper.get() ); } }
请提示,为什么valueWrapper.get()不返回null,尽管widget引用被赋予null值.
谢谢.