//: initialization/TerminationCondition.java
// Using finalize() to detect an object that
// hasn't been properly cleaned up.class Book {
boolean checkedOut = false; Book(boolean checkOut) {
checkedOut = checkOut;
} void checkIn() {
checkedOut = false;
} protected void finalize() {
if (checkedOut)
System.out.println("Error: checked out");
// Normally, you'll also do this:
// super.finalize(); // Call the base-class version
}
}public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup:
novel.checkIn();
// Drop the reference, forget to clean up:
new Book(true);
// Force garbage collection & finalization:
System.gc();
}
} /*
* Output: Error: checked out
*/// :~
问题:
运行System.gc()上一步前checkedOut的值是ture,为什么到了System.gc()就变成false了?
// Using finalize() to detect an object that
// hasn't been properly cleaned up.class Book {
boolean checkedOut = false; Book(boolean checkOut) {
checkedOut = checkOut;
} void checkIn() {
checkedOut = false;
} protected void finalize() {
if (checkedOut)
System.out.println("Error: checked out");
// Normally, you'll also do this:
// super.finalize(); // Call the base-class version
}
}public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup:
novel.checkIn();
// Drop the reference, forget to clean up:
new Book(true);
// Force garbage collection & finalization:
System.gc();
}
} /*
* Output: Error: checked out
*/// :~
问题:
运行System.gc()上一步前checkedOut的值是ture,为什么到了System.gc()就变成false了?
解决方案 »
- 如何将坐标存入数组,java数组!
- j2se中如何对MenuItem设置监听?
- 大家看看 急急急 :jsp 开发tomcat,sql server (我在旁等)
- 如果判断字符在sting内?
- 有关进度条的问题
- 请大家注意SQL注入----------小心地保护自己的数据库。那么。。
- 字符集的一些问题 !!!
- 初学java数据库编程,请教。100分相送。。。
- java的虚函数是怎样定义的,现想将类A中的变量x定义为只能给子类B与自己类中可访问,而又不能给其他类中用实例A a = new A()的a访问,可
- 鸭子也要飞
- 告诉你如何开始学java
- JSP页面中如何判断一个text中输入的内容是否是日期型
System.gc() 结果是不定的。
不能保证前句生成的对象会被回收。
到了System.gc()还是true啊
调用finalize()方法
if (checkedOut)
System.out.println("Error: checked out");
这里因为checkedOut是true才会执行输出Error: checked out啊