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