finalize()方法终结条件验证 示例代码

package Initialization;

class Tank{
	int howFull = 0;
	Tank() { this(0);}
	Tank(int fullness){
		howFull = fullness;
	}
	void sayHowFull(){
		if(howFull == 0) 
			System.out.println("Tank is empty!");
		else
			System.out.println("Tank filling status : " + howFull);
	}
	void empty(){
		howFull = 0;
	}
	protected void finalize(){
		if(howFull != 0){
			System.out.println("Error: Tank not empty." + this.howFull);
		}
		//Normally,you'll also do this:
		//Super.finalize(); //call the base-class version
	}
}

public class TankTest {
	public static void main(String[] args) {
		Tank tank1 = new Tank();
		Tank tank2 = new Tank(3);
		Tank tank3 = new Tank(5);
		tank2.empty();
		//Drop the reference,forget to cleanup:
		new Tank(6);
		new Tank(7);
		System.out.println("Check tanks:");
		System.out.println("tank1:");
		tank1.sayHowFull();
		System.out.println("tank2:");
		tank2.sayHowFull();
		System.out.println("tank3");
		tank3.sayHowFull();
		System.out.println("first forced gc()");
		System.gc();
		System.out.println("try deprecated runFinalizerOnExit(true)");
		System.runFinalizersOnExit(true);
		System.out.println("last forced gc():");
		System.gc();
	}
}

 

 

运行结果如下:

 

Check tanks:
tank1:
Tank is empty!
tank2:
Tank is empty!
tank3
Tank filling status : 5
first forced gc()
Error: Tank not empty.7
Error: Tank not empty.6
try deprecated runFinalizerOnExit(true)
last forced gc():
Error: Tank not empty.5

System.runFinalizersOnExit()方法是指当应用程序退出时对所有对象执行终结方法。包括有引用的对象和没有通过引用的对象,而System.gc()垃圾回收方法仅回收那些没有引用指向的对象。

你可能感兴趣的:(finalize)