Article 7 of objective Java study notes -- avoid using the finalizer method and javastivefinalizer
Avoid using finalizer)
Finalizer is usually unpredictable and dangerous. In general, it is unnecessary.
Do not regard finalizer as the counterpart of the destructor in C ++. In java, when an object is unavailable (that is, when no reference points to this object), the garbage collector recycles the memory resources associated with this object. Other memory resources, generally, the try-finally code block is used to complete similar work.
I. disadvantages of finalizer:
1. The disadvantage of the termination method is that it cannot be guaranteed to be executed in a timely manner.
Timely execution of the finalizer method is a major function of the JVM garbage collection method. Because different JVM garbage collection algorithms are different, the JVM will "Unintentionally" delay the execution termination method, so the execution time of the termination method is very unstable.
2. The thread priority of the finalizer method is lower than that of other threads of the current program, and the JAVA language specification does not guarantee which thread can execute the finalizer method.
3. the JAVA language specification does not guarantee the timely execution of the finalizer method, but does not guarantee the execution of the finalizer method. When the program is terminated, the finalizer method of some objects may not be executed yet. -- The finalizer method should not be used to update important persistent states.
4. System. gc and System. runFinalization do not guarantee the execution of finalizer.
5. System. runFinalizersOnExit Runtime. runFinalizersOnExit can ensure that the finalizer is executed, but these two methods have been discarded.
6. If an uncaptured exception is thrown in the finalizer method, the exception can be ignored (the warning will not be printed) and the finalizer method will terminate. This exception puts the object in the "destroyed" state. If another thread wants to use this object, uncertain behavior may occur.
7. The finalizer method may cause serious (Severe) performance loss.
2. How can I terminate object Resources in a thread without using the finalizer method?
Use the display termination method.
Requirements for display termination methods:
1. Check whether the instance has been terminated.
2. show that the termination method must record "this object is no longer valid" in a private domain"
3. After the termination method is executed, when other methods of the object are executed, check the private domain "this object is no longer valid" and throw IllegalStateException.
Example of how to display the termination method:
1. InputStream
2. OutputStream
3. java. SQL. Connection
4. java. util. Timer
Display how to use the termination method: usually used with try-finally to ensure timely termination.
FileInputStream fileInputStream = new FileInputStream ();
Try {
// Do something about fileInputStream;
} Finally {
FileInputStream. close ();
}
3. Valid use of the termination method.
1. As a security net-display when the termination method is not called
2. Terminate non-critical local resources (android JNI operation in progress)
4. Ensure that the termination process of the superclass is also executed if the termination process of the subclass is abnormal.
Because the termination method chain is not automatically executed, we need to manually ensure this.
Method 1: Use try-finalize code structure
@ Override
Protected void finalize () throws Throwable {
Try {
... // Finalize subclass state
} Finally {
Super. finalize ();
}
}
Method 2: Use finalizer guardian)
Class {
// End the Defender
Private final Object finalizerGuardian = new Object (){
@ Override
// The termination method of the Defender will be executed.
Protected void finalize (){
// Finalizer outer class --
System. out. println ("A finalize by the finalizerGuardian ");
}
};
In short,
1. Do not use the termination method unless it is a security net or to terminate non-critical local resources.
2. If necessary, use the display termination method.
2. If you cannot use finalize, do not forget to call super. finalize (). You can also consider whether to use the end method guard to enable the termination method of the parent class of the class that does not call the super. finalize () method to be executed.
References:
Effective Java version 2