1 /*
  2  * Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.misc.Blocker;
 29 import jdk.internal.vm.annotation.IntrinsicCandidate;
 30 
 31 /**
 32  * Class {@code Object} is the root of the class hierarchy.
 33  * Every class has {@code Object} as a superclass. All objects,
 34  * including arrays, implement the methods of this class.
 35  * <p>
 36  * Subclasses of {@code java.lang.Object} can be either an {@linkplain Class#isIdentity identity class}
 37  * or a {@linkplain Class#isValue value class}.
 38  * See {@jls The Java Language Specification 8.1.1.5 Value Classes}.
 39  *
 40  * @see     java.lang.Class
 41  * @since   1.0
 42  */
 43 public class Object {
 44 
 45     /**
 46      * Constructs a new object.
 47      */
 48     @IntrinsicCandidate
 49     public Object() {}
 50 
 51     /**
 52      * Returns the runtime class of this {@code Object}. The returned
 53      * {@code Class} object is the object that is locked by {@code
 54      * static synchronized} methods of the represented class.
 55      *
 56      * <p><b>The actual result type is {@code Class<? extends |X|>}
 57      * where {@code |X|} is the erasure of the static type of the
 58      * expression on which {@code getClass} is called.</b> For
 59      * example, no cast is required in this code fragment:</p>
 60      *
 61      * <p>
 62      * {@code Number n = 0;                             }<br>
 63      * {@code Class<? extends Number> c = n.getClass(); }
 64      * </p>
 65      *
 66      * @return The {@code Class} object that represents the runtime
 67      *         class of this object.
 68      * @jls 15.8.2 Class Literals
 69      */
 70     @IntrinsicCandidate
 71     public final native Class<?> getClass();
 72 
 73     /**
 74      * {@return a hash code value for this object} This method is
 75      * supported for the benefit of hash tables such as those provided by
 76      * {@link java.util.HashMap}.
 77      * <p>
 78      * The general contract of {@code hashCode} is:
 79      * <ul>
 80      * <li>Whenever it is invoked on the same object more than once during
 81      *     an execution of a Java application, the {@code hashCode} method
 82      *     must consistently return the same integer, provided no information
 83      *     used in {@code equals} comparisons on the object is modified.
 84      *     This integer need not remain consistent from one execution of an
 85      *     application to another execution of the same application.
 86      * <li>If two objects are equal according to the {@link
 87      *     #equals(Object) equals} method, then calling the {@code
 88      *     hashCode} method on each of the two objects must produce the
 89      *     same integer result.
 90      * <li>It is <em>not</em> required that if two objects are unequal
 91      *     according to the {@link #equals(Object) equals} method, then
 92      *     calling the {@code hashCode} method on each of the two objects
 93      *     must produce distinct integer results.  However, the programmer
 94      *     should be aware that producing distinct integer results for
 95      *     unequal objects may improve the performance of hash tables.
 96      * </ul>
 97      *
 98      * @implSpec
 99      * As far as is reasonably practical, the {@code hashCode} method defined
100      * by class {@code Object} returns distinct integers for distinct objects.
101      *
102      * @apiNote
103      * The {@link java.util.Objects#hash(Object...) hash} and {@link
104      * java.util.Objects#hashCode(Object) hashCode} methods of {@link
105      * java.util.Objects} can be used to help construct simple hash codes.
106      *
107      * @see     java.lang.Object#equals(java.lang.Object)
108      * @see     java.lang.System#identityHashCode
109      */
110     @IntrinsicCandidate
111     public native int hashCode();
112 
113     /**
114      * Indicates whether some other object is "equal to" this one.
115      * <p>
116      * The {@code equals} method implements an <dfn>{@index "equivalence relation"}</dfn>
117      * on non-null object references:
118      * <ul>
119      * <li>It is <i>reflexive</i>: for any non-null reference value
120      *     {@code x}, {@code x.equals(x)} should return
121      *     {@code true}.
122      * <li>It is <i>symmetric</i>: for any non-null reference values
123      *     {@code x} and {@code y}, {@code x.equals(y)}
124      *     should return {@code true} if and only if
125      *     {@code y.equals(x)} returns {@code true}.
126      * <li>It is <i>transitive</i>: for any non-null reference values
127      *     {@code x}, {@code y}, and {@code z}, if
128      *     {@code x.equals(y)} returns {@code true} and
129      *     {@code y.equals(z)} returns {@code true}, then
130      *     {@code x.equals(z)} should return {@code true}.
131      * <li>It is <i>consistent</i>: for any non-null reference values
132      *     {@code x} and {@code y}, multiple invocations of
133      *     {@code x.equals(y)} consistently return {@code true}
134      *     or consistently return {@code false}, provided no
135      *     information used in {@code equals} comparisons on the
136      *     objects is modified.
137      * <li>For any non-null reference value {@code x},
138      *     {@code x.equals(null)} should return {@code false}.
139      * </ul>
140      *
141      * <p>
142      * An equivalence relation partitions the elements it operates on
143      * into <i>equivalence classes</i>; all the members of an
144      * equivalence class are equal to each other. Members of an
145      * equivalence class are substitutable for each other, at least
146      * for some purposes.
147      *
148      * @implSpec
149      * The {@code equals} method for class {@code Object} implements
150      * the most discriminating possible equivalence relation on objects;
151      * that is, for any non-null reference values {@code x} and
152      * {@code y}, this method returns {@code true} if and only
153      * if {@code x} and {@code y} refer to the same object
154      * ({@code x == y} has the value {@code true}).
155      *
156      * In other words, under the reference equality equivalence
157      * relation, each equivalence class only has a single element.
158      *
159      * @apiNote
160      * It is generally necessary to override the {@link #hashCode() hashCode}
161      * method whenever this method is overridden, so as to maintain the
162      * general contract for the {@code hashCode} method, which states
163      * that equal objects must have equal hash codes.
164      * <p>The two-argument {@link java.util.Objects#equals(Object,
165      * Object) Objects.equals} method implements an equivalence relation
166      * on two possibly-null object references.
167      *
168      * @param   obj   the reference object with which to compare.
169      * @return  {@code true} if this object is the same as the obj
170      *          argument; {@code false} otherwise.
171      * @see     #hashCode()
172      * @see     java.util.HashMap
173      */
174     public boolean equals(Object obj) {
175         return (this == obj);
176     }
177 
178     /**
179      * Creates and returns a copy of this object.  The precise meaning
180      * of "copy" may depend on the class of the object. The general
181      * intent is that, for any object {@code x}, the expression:
182      * <blockquote>
183      * <pre>
184      * x.clone() != x</pre></blockquote>
185      * will be true, and that the expression:
186      * <blockquote>
187      * <pre>
188      * x.clone().getClass() == x.getClass()</pre></blockquote>
189      * will be {@code true}, but these are not absolute requirements.
190      * While it is typically the case that:
191      * <blockquote>
192      * <pre>
193      * x.clone().equals(x)</pre></blockquote>
194      * will be {@code true}, this is not an absolute requirement.
195      * <p>
196      * By convention, the returned object should be obtained by calling
197      * {@code super.clone}.  If a class and all of its superclasses (except
198      * {@code Object}) obey this convention, it will be the case that
199      * {@code x.clone().getClass() == x.getClass()}.
200      * <p>
201      * By convention, the object returned by this method should be independent
202      * of this object (which is being cloned).  To achieve this independence,
203      * it may be necessary to modify one or more fields of the object returned
204      * by {@code super.clone} before returning it.  Typically, this means
205      * copying any mutable objects that comprise the internal "deep structure"
206      * of the object being cloned and replacing the references to these
207      * objects with references to the copies.  If a class contains only
208      * primitive fields or references to immutable objects, then it is usually
209      * the case that no fields in the object returned by {@code super.clone}
210      * need to be modified.
211      *
212      * @implSpec
213      * The method {@code clone} for class {@code Object} performs a
214      * specific cloning operation. First, if the class of this object does
215      * not implement the interface {@code Cloneable}, then a
216      * {@code CloneNotSupportedException} is thrown. Note that all arrays
217      * are considered to implement the interface {@code Cloneable} and that
218      * the return type of the {@code clone} method of an array type {@code T[]}
219      * is {@code T[]} where T is any reference or primitive type.
220      * Otherwise, this method creates a new instance of the class of this
221      * object and initializes all its fields with exactly the contents of
222      * the corresponding fields of this object, as if by assignment; the
223      * contents of the fields are not themselves cloned. Thus, this method
224      * performs a "shallow copy" of this object, not a "deep copy" operation.
225      * <p>
226      * The class {@code Object} does not itself implement the interface
227      * {@code Cloneable}, so calling the {@code clone} method on an object
228      * whose class is {@code Object} will result in throwing an
229      * exception at run time.
230      *
231      * @return     a clone of this instance.
232      * @throws  CloneNotSupportedException  if the object's class does not
233      *               support the {@code Cloneable} interface. Subclasses
234      *               that override the {@code clone} method can also
235      *               throw this exception to indicate that an instance cannot
236      *               be cloned.
237      * @see java.lang.Cloneable
238      */
239     @IntrinsicCandidate
240     protected native Object clone() throws CloneNotSupportedException;
241 
242     /**
243      * {@return a string representation of the object}
244      *
245      * Satisfying this method's contract implies a non-{@code null}
246      * result must be returned.
247      *
248      * @apiNote
249      * In general, the
250      * {@code toString} method returns a string that
251      * "textually represents" this object. The result should
252      * be a concise but informative representation that is easy for a
253      * person to read.
254      * It is recommended that all subclasses override this method.
255      * The string output is not necessarily stable over time or across
256      * JVM invocations.
257      * @implSpec
258      * The {@code toString} method for class {@code Object}
259      * returns a string consisting of the name of the class of which the
260      * object is an instance, the at-sign character `{@code @}', and
261      * the unsigned hexadecimal representation of the hash code of the
262      * object. In other words, this method returns a string equal to the
263      * value of:
264      * {@snippet lang=java :
265      * getClass().getName() + '@' + Integer.toHexString(hashCode())
266      * }
267      * The {@link java.util.Objects#toIdentityString(Object)
268      * Objects.toIdentityString} method returns the string for an
269      * object equal to the string that would be returned if neither
270      * the {@code toString} nor {@code hashCode} methods were
271      * overridden by the object's class.
272      */
273     public String toString() {
274         return getClass().getName() + "@" + Integer.toHexString(hashCode());
275     }
276 
277     /**
278      * Wakes up a single thread that is waiting on this object's
279      * monitor. If any threads are waiting on this object, one of them
280      * is chosen to be awakened. The choice is arbitrary and occurs at
281      * the discretion of the implementation. A thread waits on an object's
282      * monitor by calling one of the {@code wait} methods.
283      * <p>
284      * The awakened thread will not be able to proceed until the current
285      * thread relinquishes the lock on this object. The awakened thread will
286      * compete in the usual manner with any other threads that might be
287      * actively competing to synchronize on this object; for example, the
288      * awakened thread enjoys no reliable privilege or disadvantage in being
289      * the next thread to lock this object.
290      * <p>
291      * This method should only be called by a thread that is the owner
292      * of this object's monitor. A thread becomes the owner of the
293      * object's monitor in one of three ways:
294      * <ul>
295      * <li>By executing a synchronized instance method of that object.
296      * <li>By executing the body of a {@code synchronized} statement
297      *     that synchronizes on the object.
298      * <li>For objects of type {@code Class,} by executing a
299      *     static synchronized method of that class.
300      * </ul>
301      * <p>
302      * Only one thread at a time can own an object's monitor.
303      * <div class="preview-block">
304      *      <div class="preview-comment">
305      *          If this object is a {@linkplain Class#isValue() value object},
306      *          it does does not have a monitor, an {@code IllegalMonitorStateException} is thrown.
307      *      </div>
308      * </div>
309      *
310      * @throws  IllegalMonitorStateException  if the current thread is not
311      *               the owner of this object's monitor or
312      *               if this object is a {@linkplain Class#isValue() value object}.
313      * @see        java.lang.Object#notifyAll()
314      * @see        java.lang.Object#wait()
315      */
316     @IntrinsicCandidate
317     public final native void notify();
318 
319     /**
320      * Wakes up all threads that are waiting on this object's monitor. A
321      * thread waits on an object's monitor by calling one of the
322      * {@code wait} methods.
323      * <p>
324      * The awakened threads will not be able to proceed until the current
325      * thread relinquishes the lock on this object. The awakened threads
326      * will compete in the usual manner with any other threads that might
327      * be actively competing to synchronize on this object; for example,
328      * the awakened threads enjoy no reliable privilege or disadvantage in
329      * being the next thread to lock this object.
330      * <p>
331      * This method should only be called by a thread that is the owner
332      * of this object's monitor. See the {@code notify} method for a
333      * description of the ways in which a thread can become the owner of
334      * a monitor.
335      *
336      * <div class="preview-block">
337      *      <div class="preview-comment">
338      *          If this object is a {@linkplain Class#isValue() value object},
339      *          it does does not have a monitor, an {@code IllegalMonitorStateException} is thrown.
340      *      </div>
341      * </div>
342      *
343      * @throws  IllegalMonitorStateException  if the current thread is not
344      *               the owner of this object's monitor or
345      *               if this object is a {@linkplain Class#isValue() value object}.
346      * @see        java.lang.Object#notify()
347      * @see        java.lang.Object#wait()
348      */
349     @IntrinsicCandidate
350     public final native void notifyAll();
351 
352     /**
353      * Causes the current thread to wait until it is awakened, typically
354      * by being <em>notified</em> or <em>interrupted</em>.
355      * <p>
356      * In all respects, this method behaves as if {@code wait(0L, 0)}
357      * had been called. See the specification of the {@link #wait(long, int)} method
358      * for details.
359      *
360      * <div class="preview-block">
361      *      <div class="preview-comment">
362      *          If this object is a {@linkplain Class#isValue() value object},
363      *          it does does not have a monitor, an {@code IllegalMonitorStateException} is thrown.
364      *      </div>
365      * </div>
366      *
367      * @throws IllegalMonitorStateException if the current thread is not
368      *         the owner of the object's monitor or
369      *         if this object is a {@linkplain Class#isValue() value object}.
370      * @throws InterruptedException if any thread interrupted the current thread before or
371      *         while the current thread was waiting. The <em>interrupted status</em> of the
372      *         current thread is cleared when this exception is thrown.
373      * @see    #notify()
374      * @see    #notifyAll()
375      * @see    #wait(long)
376      * @see    #wait(long, int)
377      */
378     public final void wait() throws InterruptedException {
379         wait(0L);
380     }
381 
382     /**
383      * Causes the current thread to wait until it is awakened, typically
384      * by being <em>notified</em> or <em>interrupted</em>, or until a
385      * certain amount of real time has elapsed.
386      * <p>
387      * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
388      * had been called. See the specification of the {@link #wait(long, int)} method
389      * for details.
390      *
391      * <div class="preview-block">
392      *      <div class="preview-comment">
393      *          If this object is a {@linkplain Class#isValue() value object},
394      *          it does does not have a monitor, an {@code IllegalMonitorStateException} is thrown.
395      *      </div>
396      * </div>
397      *
398      * @param  timeoutMillis the maximum time to wait, in milliseconds
399      * @throws IllegalArgumentException if {@code timeoutMillis} is negative
400      * @throws IllegalMonitorStateException if the current thread is not
401      *         the owner of the object's monitor or
402      *         if this object is a {@linkplain Class#isValue() value object}.
403      * @throws InterruptedException if any thread interrupted the current thread before or
404      *         while the current thread was waiting. The <em>interrupted status</em> of the
405      *         current thread is cleared when this exception is thrown.
406      * @see    #notify()
407      * @see    #notifyAll()
408      * @see    #wait()
409      * @see    #wait(long, int)
410      */
411     public final void wait(long timeoutMillis) throws InterruptedException {
412         if (!Thread.currentThread().isVirtual()) {
413             wait0(timeoutMillis);
414             return;
415         }
416 
417         // virtual thread waiting
418         boolean attempted = Blocker.begin();
419         try {
420             wait0(timeoutMillis);
421         } catch (InterruptedException e) {
422             // virtual thread's interrupt status needs to be cleared
423             Thread.currentThread().getAndClearInterrupt();
424             throw e;
425         } finally {
426             Blocker.end(attempted);
427         }
428     }
429 
430     // final modifier so method not in vtable
431     private final native void wait0(long timeoutMillis) throws InterruptedException;
432 
433     /**
434      * Causes the current thread to wait until it is awakened, typically
435      * by being <em>notified</em> or <em>interrupted</em>, or until a
436      * certain amount of real time has elapsed.
437      * <p>
438      * The current thread must own this object's monitor lock. See the
439      * {@link #notify notify} method for a description of the ways in which
440      * a thread can become the owner of a monitor lock.
441      * <p>
442      * This method causes the current thread (referred to here as <var>T</var>) to
443      * place itself in the wait set for this object and then to relinquish any
444      * and all synchronization claims on this object. Note that only the locks
445      * on this object are relinquished; any other objects on which the current
446      * thread may be synchronized remain locked while the thread waits.
447      * <p>
448      * Thread <var>T</var> then becomes disabled for thread scheduling purposes
449      * and lies dormant until one of the following occurs:
450      * <ul>
451      * <li>Some other thread invokes the {@code notify} method for this
452      * object and thread <var>T</var> happens to be arbitrarily chosen as
453      * the thread to be awakened.
454      * <li>Some other thread invokes the {@code notifyAll} method for this
455      * object.
456      * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
457      * thread <var>T</var>.
458      * <li>The specified amount of real time has elapsed, more or less.
459      * The amount of real time, in nanoseconds, is given by the expression
460      * {@code 1000000 * timeoutMillis + nanos}. If {@code timeoutMillis} and {@code nanos}
461      * are both zero, then real time is not taken into consideration and the
462      * thread waits until awakened by one of the other causes.
463      * <li>Thread <var>T</var> is awakened spuriously. (See below.)
464      * </ul>
465      * <p>
466      * The thread <var>T</var> is then removed from the wait set for this
467      * object and re-enabled for thread scheduling. It competes in the
468      * usual manner with other threads for the right to synchronize on the
469      * object; once it has regained control of the object, all its
470      * synchronization claims on the object are restored to the status quo
471      * ante - that is, to the situation as of the time that the {@code wait}
472      * method was invoked. Thread <var>T</var> then returns from the
473      * invocation of the {@code wait} method. Thus, on return from the
474      * {@code wait} method, the synchronization state of the object and of
475      * thread {@code T} is exactly as it was when the {@code wait} method
476      * was invoked.
477      * <p>
478      * A thread can wake up without being notified, interrupted, or timing out, a
479      * so-called <em>spurious wakeup</em>.  While this will rarely occur in practice,
480      * applications must guard against it by testing for the condition that should
481      * have caused the thread to be awakened, and continuing to wait if the condition
482      * is not satisfied. See the example below.
483      * <p>
484      * For more information on this topic, see section 14.2,
485      * "Condition Queues," in Brian Goetz and others' <cite>Java Concurrency
486      * in Practice</cite> (Addison-Wesley, 2006) or Item 81 in Joshua
487      * Bloch's <cite>Effective Java, Third Edition</cite> (Addison-Wesley,
488      * 2018).
489      * <p>
490      * If the current thread is {@linkplain java.lang.Thread#interrupt() interrupted}
491      * by any thread before or while it is waiting, then an {@code InterruptedException}
492      * is thrown.  The <em>interrupted status</em> of the current thread is cleared when
493      * this exception is thrown. This exception is not thrown until the lock status of
494      * this object has been restored as described above.
495      *
496      * @apiNote
497      * The recommended approach to waiting is to check the condition being awaited in
498      * a {@code while} loop around the call to {@code wait}, as shown in the example
499      * below. Among other things, this approach avoids problems that can be caused
500      * by spurious wakeups.
501      *
502      * {@snippet lang=java :
503      *     synchronized (obj) {
504      *         while ( <condition does not hold and timeout not exceeded> ) {
505      *             long timeoutMillis = ... ; // recompute timeout values
506      *             int nanos = ... ;
507      *             obj.wait(timeoutMillis, nanos);
508      *         }
509      *         ... // Perform action appropriate to condition or timeout
510      *     }
511      * }
512      *
513      * <div class="preview-block">
514      *      <div class="preview-comment">
515      *          If this object is a {@linkplain Class#isValue() value object},
516      *          it does does not have a monitor, an {@code IllegalMonitorStateException} is thrown.
517      *      </div>
518      * </div>
519      * @param  timeoutMillis the maximum time to wait, in milliseconds
520      * @param  nanos   additional time, in nanoseconds, in the range 0-999999 inclusive
521      * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
522      *         or if the value of {@code nanos} is out of range
523      * @throws IllegalMonitorStateException if the current thread is not
524      *         the owner of the object's monitor or
525      *         if this object is a {@linkplain Class#isValue() value object}.
526      * @throws InterruptedException if any thread interrupted the current thread before or
527      *         while the current thread was waiting. The <em>interrupted status</em> of the
528      *         current thread is cleared when this exception is thrown.
529      * @see    #notify()
530      * @see    #notifyAll()
531      * @see    #wait()
532      * @see    #wait(long)
533      */
534     public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
535         if (timeoutMillis < 0) {
536             throw new IllegalArgumentException("timeoutMillis value is negative");
537         }
538 
539         if (nanos < 0 || nanos > 999999) {
540             throw new IllegalArgumentException(
541                                 "nanosecond timeout value out of range");
542         }
543 
544         if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
545             timeoutMillis++;
546         }
547 
548         wait(timeoutMillis);
549     }
550 
551     /**
552      * Called by the garbage collector on an object when garbage collection
553      * determines that there are no more references to the object.
554      * A subclass overrides the {@code finalize} method to dispose of
555      * system resources or to perform other cleanup.
556      * <p>
557      * <b>When running in a Java virtual machine in which finalization has been
558      * disabled or removed, the garbage collector will never call
559      * {@code finalize()}. In a Java virtual machine in which finalization is
560      * enabled, the garbage collector might call {@code finalize} only after an
561      * indefinite delay.</b>
562      * <p>
563      * The general contract of {@code finalize} is that it is invoked
564      * if and when the Java virtual
565      * machine has determined that there is no longer any
566      * means by which this object can be accessed by any thread that has
567      * not yet died, except as a result of an action taken by the
568      * finalization of some other object or class which is ready to be
569      * finalized. The {@code finalize} method may take any action, including
570      * making this object available again to other threads; the usual purpose
571      * of {@code finalize}, however, is to perform cleanup actions before
572      * the object is irrevocably discarded. For example, the finalize method
573      * for an object that represents an input/output connection might perform
574      * explicit I/O transactions to break the connection before the object is
575      * permanently discarded.
576      * <p>
577      * The {@code finalize} method of class {@code Object} performs no
578      * special action; it simply returns normally. Subclasses of
579      * {@code Object} may override this definition.
580      * <p>
581      * The Java programming language does not guarantee which thread will
582      * invoke the {@code finalize} method for any given object. It is
583      * guaranteed, however, that the thread that invokes finalize will not
584      * be holding any user-visible synchronization locks when finalize is
585      * invoked. If an uncaught exception is thrown by the finalize method,
586      * the exception is ignored and finalization of that object terminates.
587      * <p>
588      * After the {@code finalize} method has been invoked for an object, no
589      * further action is taken until the Java virtual machine has again
590      * determined that there is no longer any means by which this object can
591      * be accessed by any thread that has not yet died, including possible
592      * actions by other objects or classes which are ready to be finalized,
593      * at which point the object may be discarded.
594      * <p>
595      * The {@code finalize} method is never invoked more than once by a Java
596      * virtual machine for any given object.
597      * <p>
598      * Any exception thrown by the {@code finalize} method causes
599      * the finalization of this object to be halted, but is otherwise
600      * ignored.
601      *
602      * @apiNote
603      * Classes that embed non-heap resources have many options
604      * for cleanup of those resources. The class must ensure that the
605      * lifetime of each instance is longer than that of any resource it embeds.
606      * {@link java.lang.ref.Reference#reachabilityFence} can be used to ensure that
607      * objects remain reachable while resources embedded in the object are in use.
608      * <p>
609      * A subclass should avoid overriding the {@code finalize} method
610      * unless the subclass embeds non-heap resources that must be cleaned up
611      * before the instance is collected.
612      * Finalizer invocations are not automatically chained, unlike constructors.
613      * If a subclass overrides {@code finalize} it must invoke the superclass
614      * finalizer explicitly.
615      * To guard against exceptions prematurely terminating the finalize chain,
616      * the subclass should use a {@code try-finally} block to ensure
617      * {@code super.finalize()} is always invoked. For example,
618      * {@snippet lang="java":
619      *     @Override
620      *     protected void finalize() throws Throwable {
621      *         try {
622      *             ... // cleanup subclass state
623      *         } finally {
624      *             super.finalize();
625      *         }
626      *     }
627      * }
628      *
629      * @deprecated Finalization is deprecated and subject to removal in a future
630      * release. The use of finalization can lead to problems with security,
631      * performance, and reliability.
632      * See <a href="https://openjdk.org/jeps/421">JEP 421</a> for
633      * discussion and alternatives.
634      * <p>
635      * Subclasses that override {@code finalize} to perform cleanup should use
636      * alternative cleanup mechanisms and remove the {@code finalize} method.
637      * Use {@link java.lang.ref.Cleaner} and
638      * {@link java.lang.ref.PhantomReference} as safer ways to release resources
639      * when an object becomes unreachable. Alternatively, add a {@code close}
640      * method to explicitly release resources, and implement
641      * {@code AutoCloseable} to enable use of the {@code try}-with-resources
642      * statement.
643      * <p>
644      * This method will remain in place until finalizers have been removed from
645      * most existing code.
646      *
647      * @throws Throwable the {@code Exception} raised by this method
648      * @see java.lang.ref.WeakReference
649      * @see java.lang.ref.PhantomReference
650      * @jls 12.6 Finalization of Class Instances
651      */
652     @Deprecated(since="9", forRemoval=true)
653     protected void finalize() throws Throwable { }
654 }