Effective Java 第七条:避免使用终结方法
- 终结方法(finilizer)通常是:
不可预测的危险的一般情况下是不必要的 - 终结方法的缺点:
不保证被及时执行 - 不应该依赖终结方法来更新重要的持久状态
- 使用终结方法有严重的性能损失
- 显式终结方法:
InputStreamOutputStreamjava.sql.Connectionclose
Foo foo = new Foo(...);
try {
// Do what must be down with foo
...
} finally {
foo.terminate(); //Explicit termination method
}