简单了解finalize()与垃圾回收
public class A
{
public static A a1 = new A();
public static void main(String [] args ) throws Exception{
a1 = null;
System.gc();
Thread.sleep(300);
System.out.println(a1);
a1 = null;
System.gc();
Thread.sleep(300);
System.out.println(a1);
}
protected void finalize() throws Throwable {
a1 = this;
}
}
------解决方案--------------------
gc拥有很不确定性。
你可以看官方文档,因为你不知道gc什么时候运行,什么时候进行垃圾回收