finalize与gc问题
class Chair
{
public static long created = 0;
public static long finalized = 0;
public static boolean gcrun = false;
public static boolean f = false;
public long i = 0;
Chair(){
i = ++created;
if (i == 1)
{
System.out.println("Creating start!");
}
//System.out.print(".");
}
public void finalize(){
finalized++;
//System.out.print("-");
if (!gcrun)
{
gcrun = true;
System.out.println("Finalized start!");
}
if (i == 47)
{
f = true;
System.out.println("finalized 47 chair,"+created+" created,stop create!");
}
if (finalized >= created)
{
System.out.println("All chair is finalized!");
}
}
}
public class Gc
{
public static void main(String args[]){
while(Chair.f == false){
new Chair();
new String("take a few space");
}
System.out.println(Chair.created+" chair created,"+Chair.finalized+" chair finalized!");
System.out.println("Bye");
}
}
1.看了think in java中关于gc和finalize的介绍,根据书上的例子写了这个程序,但是发现All chair is finalized一直在打印出来,就是说一直在调用finalize,这让我很不解,哪位帮我看看,是什么原因
2.我如果把注解的部分打开,有可以得到正常结果,非常奇怪.
JDK是1.4.2
先谢谢大家了.