@@ -478,6 +478,12 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
478
478
* A subclass overrides the {@code finalize} method to dispose of
479
479
* system resources or to perform other cleanup.
480
480
* <p>
481
+ * <b>When running in a Java virtual machine in which finalization has been
482
+ * disabled or removed, the garbage collector will never call
483
+ * {@code finalize()}. In a Java virtual machine in which finalization is
484
+ * enabled, the garbage collector might call {@code finalize} only after an
485
+ * indefinite delay.</b>
486
+ * <p>
481
487
* The general contract of {@code finalize} is that it is invoked
482
488
* if and when the Java virtual
483
489
* machine has determined that there is no longer any
@@ -543,27 +549,29 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
543
549
* }
544
550
* }</pre>
545
551
*
546
- * @deprecated The finalization mechanism is inherently problematic.
547
- * Finalization can lead to performance issues, deadlocks, and hangs.
548
- * Errors in finalizers can lead to resource leaks; there is no way to cancel
549
- * finalization if it is no longer necessary; and no ordering is specified
550
- * among calls to {@code finalize} methods of different objects.
551
- * Furthermore, there are no guarantees regarding the timing of finalization.
552
- * The {@code finalize} method might be called on a finalizable object
553
- * only after an indefinite delay, if at all.
554
- *
555
- * Classes whose instances hold non-heap resources should provide a method
556
- * to enable explicit release of those resources, and they should also
557
- * implement {@link AutoCloseable} if appropriate.
558
- * The {@link java.lang.ref.Cleaner} and {@link java.lang.ref.PhantomReference}
559
- * provide more flexible and efficient ways to release resources when an object
560
- * becomes unreachable.
552
+ * @deprecated Finalization is deprecated and subject to removal in a future
553
+ * release. The use of finalization can lead to problems with security,
554
+ * performance, and reliability.
555
+ * See <a href="https://openjdk.java.net/jeps/421">JEP 421</a> for
556
+ * discussion and alternatives.
557
+ * <p>
558
+ * Subclasses that override {@code finalize} to perform cleanup should use
559
+ * alternative cleanup mechanisms and remove the {@code finalize} method.
560
+ * Use {@link java.lang.ref.Cleaner} and
561
+ * {@link java.lang.ref.PhantomReference} as safer ways to release resources
562
+ * when an object becomes unreachable. Alternatively, add a {@code close}
563
+ * method to explicitly release resources, and implement
564
+ * {@code AutoCloseable} to enable use of the {@code try}-with-resources
565
+ * statement.
566
+ * <p>
567
+ * This method will remain in place until finalizers have been removed from
568
+ * most existing code.
561
569
*
562
570
* @throws Throwable the {@code Exception} raised by this method
563
571
* @see java.lang.ref.WeakReference
564
572
* @see java.lang.ref.PhantomReference
565
573
* @jls 12.6 Finalization of Class Instances
566
574
*/
567
- @ Deprecated (since ="9" )
575
+ @ Deprecated (since ="9" , forRemoval = true )
568
576
protected void finalize () throws Throwable { }
569
577
}
0 commit comments