关于debug

myj870517 2008-09-11 05:16:33
//: 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了?
...全文
92 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gesanri 2008-09-15
  • 打赏
  • 举报
回复
System.gc()上一步前checkedOut的值是true
到了System.gc()还是true啊
调用finalize()方法
if (checkedOut)
System.out.println("Error: checked out");
这里因为checkedOut是true才会执行输出Error: checked out啊
greathawker 2008-09-14
  • 打赏
  • 举报
回复

System.gc() 结果是不定的。
不能保证前句生成的对象会被回收。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧