www.pudn.com > TIJc04.rar > TerminationCondition.java


//: c04:TerminationCondition.java 
// Using finalize() to detect an object that 
// hasn't been properly cleaned up. 
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 
// www.BruceEckel.com. See copyright notice in CopyRight.txt. 
import com.bruceeckel.simpletest.*; 
 
class Book { 
  boolean checkedOut = false; 
  Book(boolean checkOut) { 
    checkedOut = checkOut; 
  } 
  void checkIn() { 
    checkedOut = false; 
  } 
  public void finalize() { 
    if(checkedOut) 
      System.out.println("Error: checked out"); 
  } 
} 
 
public class TerminationCondition { 
  static Test monitor = new Test(); 
  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(); 
    monitor.expect(new String[] { 
      "Error: checked out"}, Test.WAIT); 
  } 
} ///:~