class Garbage
{
int index;
static int count;
Garbage()
{
count++;
System.out.println("object"+count+"construct"+"创建");
setID(count);
} void setID(int id)
{
index=id;
} protected void finalize()
{
System.out.println("object"+index+"is reclaimed"+"消毁");
} public static void main(String[] args)
{
new Garbage();
new Garbage();
new Garbage();
new Garbage();
System.gc();
}
}
我这机子编译生成了4个对象,只有序号4对象销毁了。
{
int index;
static int count;
Garbage()
{
count++;
System.out.println("object"+count+"construct"+"创建");
setID(count);
} void setID(int id)
{
index=id;
} protected void finalize()
{
System.out.println("object"+index+"is reclaimed"+"消毁");
} public static void main(String[] args)
{
new Garbage();
new Garbage();
new Garbage();
new Garbage();
System.gc();
}
}
我这机子编译生成了4个对象,只有序号4对象销毁了。
不过运行LZ的程序居然发现4个对象都有finalize调用。
如果LZ只看到第4个,能否在main方法最后加Thread.sleep(3000);这样的语句让程序等待一下看看结果呢?