1 /*
2 * Copyright (c) 1994, 2025, 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.vm.annotation.AOTSafeClassInitializer;
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 *
36 * @see java.lang.Class
37 * @since 1.0
38 */
39 @AOTSafeClassInitializer // for hierarchy checks
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
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 *
242 * Satisfying this method's contract implies a non-{@code null}
280 * <p>
281 * The awakened thread will not be able to proceed until the current
282 * thread relinquishes the lock on this object. The awakened thread will
283 * compete in the usual manner with any other threads that might be
284 * actively competing to synchronize on this object; for example, the
285 * awakened thread enjoys no reliable privilege or disadvantage in being
286 * the next thread to lock this object.
287 * <p>
288 * This method should only be called by a thread that is the owner
289 * of this object's monitor. A thread becomes the owner of the
290 * object's monitor in one of three ways:
291 * <ul>
292 * <li>By executing a synchronized instance method of that object.
293 * <li>By executing the body of a {@code synchronized} statement
294 * that synchronizes on the object.
295 * <li>For objects of type {@code Class,} by executing a
296 * static synchronized method of that class.
297 * </ul>
298 * <p>
299 * Only one thread at a time can own an object's monitor.
300 *
301 * @throws IllegalMonitorStateException if the current thread is not
302 * the owner of this object's monitor.
303 * @see java.lang.Object#notifyAll()
304 * @see java.lang.Object#wait()
305 */
306 @IntrinsicCandidate
307 public final native void notify();
308
309 /**
310 * Wakes up all threads that are waiting on this object's monitor. A
311 * thread waits on an object's monitor by calling one of the
312 * {@code wait} methods.
313 * <p>
314 * The awakened threads will not be able to proceed until the current
315 * thread relinquishes the lock on this object. The awakened threads
316 * will compete in the usual manner with any other threads that might
317 * be actively competing to synchronize on this object; for example,
318 * the awakened threads enjoy no reliable privilege or disadvantage in
319 * being the next thread to lock this object.
320 * <p>
321 * This method should only be called by a thread that is the owner
322 * of this object's monitor. See the {@code notify} method for a
323 * description of the ways in which a thread can become the owner of
324 * a monitor.
325 *
326 * @throws IllegalMonitorStateException if the current thread is not
327 * the owner of this object's monitor.
328 * @see java.lang.Object#notify()
329 * @see java.lang.Object#wait()
330 */
331 @IntrinsicCandidate
332 public final native void notifyAll();
333
334 /**
335 * Causes the current thread to wait until it is awakened, typically
336 * by being <em>notified</em> or <em>interrupted</em>.
337 * <p>
338 * In all respects, this method behaves as if {@code wait(0L, 0)}
339 * had been called. See the specification of the {@link #wait(long, int)} method
340 * for details.
341 *
342 * @throws IllegalMonitorStateException if the current thread is not
343 * the owner of the object's monitor
344 * @throws InterruptedException if any thread interrupted the current thread before or
345 * while the current thread was waiting. The <em>interrupted status</em> of the
346 * current thread is cleared when this exception is thrown.
347 * @see #notify()
348 * @see #notifyAll()
349 * @see #wait(long)
350 * @see #wait(long, int)
351 */
352 public final void wait() throws InterruptedException {
353 wait(0L);
354 }
355
356 /**
357 * Causes the current thread to wait until it is awakened, typically
358 * by being <em>notified</em> or <em>interrupted</em>, or until a
359 * certain amount of real time has elapsed.
360 * <p>
361 * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
362 * had been called. See the specification of the {@link #wait(long, int)} method
363 * for details.
364 *
365 * @param timeoutMillis the maximum time to wait, in milliseconds
366 * @throws IllegalArgumentException if {@code timeoutMillis} is negative
367 * @throws IllegalMonitorStateException if the current thread is not
368 * the owner of the object's monitor
369 * @throws InterruptedException if any thread interrupted the current thread before or
370 * while the current thread was waiting. The <em>interrupted status</em> of the
371 * current thread is cleared when this exception is thrown.
372 * @see #notify()
373 * @see #notifyAll()
374 * @see #wait()
375 * @see #wait(long, int)
376 */
377 public final void wait(long timeoutMillis) throws InterruptedException {
378 if (timeoutMillis < 0) {
379 throw new IllegalArgumentException("timeout value is negative");
380 }
381
382 if (Thread.currentThread() instanceof VirtualThread vthread) {
383 try {
384 wait0(timeoutMillis);
441 * was invoked.
442 * <p>
443 * A thread can wake up without being notified, interrupted, or timing out, a
444 * so-called <em>spurious wakeup</em>. While this will rarely occur in practice,
445 * applications must guard against it by testing for the condition that should
446 * have caused the thread to be awakened, and continuing to wait if the condition
447 * is not satisfied. See the example below.
448 * <p>
449 * For more information on this topic, see section 14.2,
450 * "Condition Queues," in Brian Goetz and others' <cite>Java Concurrency
451 * in Practice</cite> (Addison-Wesley, 2006) or Item 81 in Joshua
452 * Bloch's <cite>Effective Java, Third Edition</cite> (Addison-Wesley,
453 * 2018).
454 * <p>
455 * If the current thread is {@linkplain java.lang.Thread#interrupt() interrupted}
456 * by any thread before or while it is waiting, then an {@code InterruptedException}
457 * is thrown. The <em>interrupted status</em> of the current thread is cleared when
458 * this exception is thrown. This exception is not thrown until the lock status of
459 * this object has been restored as described above.
460 *
461 * @apiNote
462 * The recommended approach to waiting is to check the condition being awaited in
463 * a {@code while} loop around the call to {@code wait}, as shown in the example
464 * below. Among other things, this approach avoids problems that can be caused
465 * by spurious wakeups.
466 *
467 * {@snippet lang=java :
468 * synchronized (obj) {
469 * while ( <condition does not hold and timeout not exceeded> ) {
470 * long timeoutMillis = ... ; // recompute timeout values
471 * int nanos = ... ;
472 * obj.wait(timeoutMillis, nanos);
473 * }
474 * ... // Perform action appropriate to condition or timeout
475 * }
476 * }
477 *
478 * @param timeoutMillis the maximum time to wait, in milliseconds
479 * @param nanos additional time, in nanoseconds, in the range 0-999999 inclusive
480 * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
490 * @see #wait(long)
491 */
492 public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
493 if (timeoutMillis < 0) {
494 throw new IllegalArgumentException("timeoutMillis value is negative");
495 }
496
497 if (nanos < 0 || nanos > 999999) {
498 throw new IllegalArgumentException(
499 "nanosecond timeout value out of range");
500 }
501
502 if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
503 timeoutMillis++;
504 }
505
506 wait(timeoutMillis);
507 }
508
509 /**
510 * Called by the garbage collector on an object when garbage collection
511 * determines that there are no more references to the object.
512 * A subclass overrides the {@code finalize} method to dispose of
513 * system resources or to perform other cleanup.
514 * <p>
515 * <b>When running in a Java virtual machine in which finalization has been
516 * disabled or removed, the garbage collector will never call
517 * {@code finalize()}. In a Java virtual machine in which finalization is
518 * enabled, the garbage collector might call {@code finalize} only after an
519 * indefinite delay.</b>
520 * <p>
521 * The general contract of {@code finalize} is that it is invoked
522 * if and when the Java virtual
523 * machine has determined that there is no longer any
524 * means by which this object can be accessed by any thread that has
525 * not yet died, except as a result of an action taken by the
526 * finalization of some other object or class which is ready to be
527 * finalized. The {@code finalize} method may take any action, including
528 * making this object available again to other threads; the usual purpose
529 * of {@code finalize}, however, is to perform cleanup actions before
530 * the object is irrevocably discarded. For example, the finalize method
531 * for an object that represents an input/output connection might perform
532 * explicit I/O transactions to break the connection before the object is
533 * permanently discarded.
534 * <p>
535 * The {@code finalize} method of class {@code Object} performs no
536 * special action; it simply returns normally. Subclasses of
537 * {@code Object} may override this definition.
|
1 /*
2 * Copyright (c) 1994, 2026, 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.vm.annotation.AOTSafeClassInitializer;
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 *
36 * <div class="preview-block">
37 * <div class="preview-comment">
38 * When preview features are enabled, subclasses of {@code java.lang.Object}
39 * are either {@linkplain Class#isValue value classes} or identity classes.
40 * A <em>value object</em> is an instance of a non-abstract value class. All
41 * other objects, including arrays, are <em>identity objects</em>. See
42 * The Java Language Specification {@jls value-objects-8.1.1.5 Value Classes}.
43 * <p>
44 * When preview features are disabled, all classes are identity classes and
45 * all objects are identity objects.
46 * <p>
47 * It is not possible to synchronize on a value object. An attempt to {@code
48 * synchronize} on a value object causes {@link IdentityException} to be thrown.
49 * <p>
50 * The {@link #finalize()} method of a value class will never be invoked by
51 * the garbage collector.
52 * <p>
53 * A {@linkplain java.lang.ref.Reference Reference object} can only refer to an
54 * object with identity. Creating a reference object with a value object as
55 * the referent throws {@code IdentityException}.
56 * </div>
57 * </div>
58 *
59 * @see java.lang.Class
60 * @since 1.0
61 */
62 @AOTSafeClassInitializer // for hierarchy checks
63 public class Object {
64
65 /**
66 * Constructs a new object.
67 */
68 @IntrinsicCandidate
69 public Object() {}
70
71 /**
72 * Returns the runtime class of this {@code Object}. The returned
73 * {@code Class} object is the object that is locked by {@code
74 * static synchronized} methods of the represented class.
75 *
76 * <p><b>The actual result type is {@code Class<? extends |X|>}
77 * where {@code |X|} is the erasure of the static type of the
78 * expression on which {@code getClass} is called.</b> For
153 * {@code x.equals(y)} consistently return {@code true}
154 * or consistently return {@code false}, provided no
155 * information used in {@code equals} comparisons on the
156 * objects is modified.
157 * <li>For any non-null reference value {@code x},
158 * {@code x.equals(null)} should return {@code false}.
159 * </ul>
160 *
161 * <p>
162 * An equivalence relation partitions the elements it operates on
163 * into <i>equivalence classes</i>; all the members of an
164 * equivalence class are equal to each other. Members of an
165 * equivalence class are substitutable for each other, at least
166 * for some purposes.
167 *
168 * @implSpec
169 * The {@code equals} method for class {@code Object} implements
170 * the most discriminating possible equivalence relation on objects;
171 * that is, for any non-null reference values {@code x} and
172 * {@code y}, this method returns {@code true} if and only
173 * if {@code x} and {@code y} refer to the same identity object or
174 * indistinguishable value objects ({@code x == y} has the value
175 * {@code true}).
176 * <p>
177 * In other words, under the object equality equivalence
178 * relation, each equivalence class only has a single element.
179 *
180 * @apiNote
181 * It is generally necessary to override the {@link #hashCode() hashCode}
182 * method whenever this method is overridden, so as to maintain the
183 * general contract for the {@code hashCode} method, which states
184 * that equal objects must have equal hash codes.
185 * <p>The two-argument {@link java.util.Objects#equals(Object,
186 * Object) Objects.equals} method implements an equivalence relation
187 * on two possibly-null object references.
188 *
189 * @param obj the reference object with which to compare.
190 * @return {@code true} if this object is the same as the obj
191 * argument; {@code false} otherwise.
192 * @see #hashCode()
193 * @see java.util.HashMap
194 */
195 public boolean equals(Object obj) {
196 return (this == obj);
197 }
198
199 /**
200 * Creates and returns a copy of this object. The precise meaning
201 * of "copy" may depend on the class of the object. The general
202 * intent is that, for any object {@code x}, the expression:
203 * <blockquote>
204 * <pre>
205 * x.clone() != x</pre></blockquote>
206 * will be true, and that the expression:
207 * <blockquote>
208 * <pre>
209 * x.clone().getClass() == x.getClass()</pre></blockquote>
210 * will also be {@code true}, but these are not absolute requirements.
211 * The clone of a value object, in particular, may be indistinguishable from
212 * the original.
213 * <p>
214 * While it is typically the case that:
215 * <blockquote>
216 * <pre>
217 * x.clone().equals(x)</pre></blockquote>
218 * will be {@code true}, this is not an absolute requirement.
219 * <p>
220 * By convention, the {@code clone} method of an identity class should return an
221 * object obtained by calling {@code super.clone}. If a class and all of its
222 * superclasses (except {@code Object}) obey this convention, it will be the
223 * case that {@code x.clone().getClass() == x.getClass()}.
224 * <p>
225 * By convention, the object returned by this method should be independent
226 * of this object (which is being cloned). To achieve this independence,
227 * it may be necessary to modify one or more fields of the object returned
228 * by {@code super.clone} before returning it. Typically, this means
229 * copying any mutable objects that comprise the internal "deep structure"
230 * of the object being cloned and replacing the references to these
231 * objects with references to the copies. If a class contains only
232 * primitive fields or references to immutable objects, then it is usually
233 * the case that no fields in the object returned by {@code super.clone}
234 * need to be modified.
235 * <p>
236 * Value classes may similarly perform deep copies of any mutable field
237 * values before constructing a new class instance with those copies.
238 *
239 * @apiNote
240 * It should be rare for new classes to implement the {@link Cloneable} interface.
241 * Copy constructors and static factory methods provide a more explicit and flexible
242 * means of creating copies, allowing classes to define and document their copying
243 * semantics without the constraints imposed by {@code Cloneable} interface and
244 * the {@code clone} method.
245 *
246 * @implSpec
247 * The method {@code clone} for class {@code Object} performs a
248 * specific cloning operation. First, if the class of this object does
249 * not implement the interface {@code Cloneable}, then a
250 * {@code CloneNotSupportedException} is thrown. Note that all arrays
251 * are considered to implement the interface {@code Cloneable} and that
252 * the return type of the {@code clone} method of an array type {@code T[]}
253 * is {@code T[]} where T is any reference or primitive type.
254 * <p>
255 * For an identity object, this method creates a new instance of the class of
256 * this object and initializes all its fields with exactly the contents of
257 * the corresponding fields of this object, as if by assignment; the
258 * contents of the fields are not themselves cloned. Thus, this method
259 * performs a "shallow copy" of this object, not a "deep copy" operation.
260 * <p>
261 * For a value object, this method returns an object that is indistinguishable
262 * from this object.
263 * <p>
264 * The class {@code Object} does not itself implement the interface
265 * {@code Cloneable}, so calling the {@code clone} method on an object
266 * whose class is {@code Object} will result in throwing an
267 * exception at run time.
268 *
269 * @return a clone of this instance.
270 * @throws CloneNotSupportedException if the object's class does not
271 * support the {@code Cloneable} interface. Subclasses
272 * that override the {@code clone} method can also
273 * throw this exception to indicate that an instance cannot
274 * be cloned.
275 * @see java.lang.Cloneable
276 */
277 @IntrinsicCandidate
278 protected native Object clone() throws CloneNotSupportedException;
279
280 /**
281 * {@return a string representation of the object}
282 *
283 * Satisfying this method's contract implies a non-{@code null}
321 * <p>
322 * The awakened thread will not be able to proceed until the current
323 * thread relinquishes the lock on this object. The awakened thread will
324 * compete in the usual manner with any other threads that might be
325 * actively competing to synchronize on this object; for example, the
326 * awakened thread enjoys no reliable privilege or disadvantage in being
327 * the next thread to lock this object.
328 * <p>
329 * This method should only be called by a thread that is the owner
330 * of this object's monitor. A thread becomes the owner of the
331 * object's monitor in one of three ways:
332 * <ul>
333 * <li>By executing a synchronized instance method of that object.
334 * <li>By executing the body of a {@code synchronized} statement
335 * that synchronizes on the object.
336 * <li>For objects of type {@code Class,} by executing a
337 * static synchronized method of that class.
338 * </ul>
339 * <p>
340 * Only one thread at a time can own an object's monitor.
341 * <div class="preview-block">
342 * <div class="preview-comment">
343 * The {@code notify} method requires that the current thread be the owner
344 * of the object's monitor. Since it is not possible to synchronize on a
345 * value object, an attempt to call this method on a value object will
346 * always fail with {@code IllegalMonitorStateException}.
347 * </div>
348 * </div>
349 *
350 * @throws IllegalMonitorStateException if the current thread is not
351 * the owner of this object's monitor.
352 * @see java.lang.Object#notifyAll()
353 * @see java.lang.Object#wait()
354 */
355 @IntrinsicCandidate
356 public final native void notify();
357
358 /**
359 * Wakes up all threads that are waiting on this object's monitor. A
360 * thread waits on an object's monitor by calling one of the
361 * {@code wait} methods.
362 * <p>
363 * The awakened threads will not be able to proceed until the current
364 * thread relinquishes the lock on this object. The awakened threads
365 * will compete in the usual manner with any other threads that might
366 * be actively competing to synchronize on this object; for example,
367 * the awakened threads enjoy no reliable privilege or disadvantage in
368 * being the next thread to lock this object.
369 * <p>
370 * This method should only be called by a thread that is the owner
371 * of this object's monitor. See the {@code notify} method for a
372 * description of the ways in which a thread can become the owner of
373 * a monitor.
374 *
375 * <div class="preview-block">
376 * <div class="preview-comment">
377 * The {@code notifyAll} method requires that the current thread be the owner
378 * of the object's monitor. Since it is not possible to synchronize on a
379 * value object, an attempt to call this method on a value object will
380 * always fail with {@code IllegalMonitorStateException}.
381 * </div>
382 * </div>
383 *
384 * @throws IllegalMonitorStateException if the current thread is not
385 * the owner of this object's monitor.
386 * @see java.lang.Object#notify()
387 * @see java.lang.Object#wait()
388 */
389 @IntrinsicCandidate
390 public final native void notifyAll();
391
392 /**
393 * Causes the current thread to wait until it is awakened, typically
394 * by being <em>notified</em> or <em>interrupted</em>.
395 * <p>
396 * In all respects, this method behaves as if {@code wait(0L, 0)}
397 * had been called. See the specification of the {@link #wait(long, int)} method
398 * for details.
399 *
400 * <div class="preview-block">
401 * <div class="preview-comment">
402 * The {@code wait} method requires that the current thread be the owner
403 * of the object's monitor. Since it is not possible to synchronize on a
404 * value object, an attempt to call this method on a value object will
405 * always fail with {@code IllegalMonitorStateException}.
406 * </div>
407 * </div>
408 *
409 * @throws IllegalMonitorStateException if the current thread is not
410 * the owner of the object's monitor
411 * @throws InterruptedException if any thread interrupted the current thread before or
412 * while the current thread was waiting. The <em>interrupted status</em> of the
413 * current thread is cleared when this exception is thrown.
414 * @see #notify()
415 * @see #notifyAll()
416 * @see #wait(long)
417 * @see #wait(long, int)
418 */
419 public final void wait() throws InterruptedException {
420 wait(0L);
421 }
422
423 /**
424 * Causes the current thread to wait until it is awakened, typically
425 * by being <em>notified</em> or <em>interrupted</em>, or until a
426 * certain amount of real time has elapsed.
427 * <p>
428 * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
429 * had been called. See the specification of the {@link #wait(long, int)} method
430 * for details.
431 *
432 * <div class="preview-block">
433 * <div class="preview-comment">
434 * The {@code wait} method requires that the current thread be the owner
435 * of the object's monitor. Since it is not possible to synchronize on a
436 * value object, an attempt to call this method on a value object will
437 * always fail with {@code IllegalMonitorStateException}.
438 * </div>
439 * </div>
440 *
441 * @param timeoutMillis the maximum time to wait, in milliseconds
442 * @throws IllegalArgumentException if {@code timeoutMillis} is negative
443 * @throws IllegalMonitorStateException if the current thread is not
444 * the owner of the object's monitor
445 * @throws InterruptedException if any thread interrupted the current thread before or
446 * while the current thread was waiting. The <em>interrupted status</em> of the
447 * current thread is cleared when this exception is thrown.
448 * @see #notify()
449 * @see #notifyAll()
450 * @see #wait()
451 * @see #wait(long, int)
452 */
453 public final void wait(long timeoutMillis) throws InterruptedException {
454 if (timeoutMillis < 0) {
455 throw new IllegalArgumentException("timeout value is negative");
456 }
457
458 if (Thread.currentThread() instanceof VirtualThread vthread) {
459 try {
460 wait0(timeoutMillis);
517 * was invoked.
518 * <p>
519 * A thread can wake up without being notified, interrupted, or timing out, a
520 * so-called <em>spurious wakeup</em>. While this will rarely occur in practice,
521 * applications must guard against it by testing for the condition that should
522 * have caused the thread to be awakened, and continuing to wait if the condition
523 * is not satisfied. See the example below.
524 * <p>
525 * For more information on this topic, see section 14.2,
526 * "Condition Queues," in Brian Goetz and others' <cite>Java Concurrency
527 * in Practice</cite> (Addison-Wesley, 2006) or Item 81 in Joshua
528 * Bloch's <cite>Effective Java, Third Edition</cite> (Addison-Wesley,
529 * 2018).
530 * <p>
531 * If the current thread is {@linkplain java.lang.Thread#interrupt() interrupted}
532 * by any thread before or while it is waiting, then an {@code InterruptedException}
533 * is thrown. The <em>interrupted status</em> of the current thread is cleared when
534 * this exception is thrown. This exception is not thrown until the lock status of
535 * this object has been restored as described above.
536 *
537 * <div class="preview-block">
538 * <div class="preview-comment">
539 * The {@code wait} method requires that the current thread be the owner
540 * of the object's monitor. Since it is not possible to synchronize on a
541 * value object, an attempt to call this method on a value object will
542 * always fail with {@code IllegalMonitorStateException}.
543 * </div>
544 * </div>
545 *
546 * @apiNote
547 * The recommended approach to waiting is to check the condition being awaited in
548 * a {@code while} loop around the call to {@code wait}, as shown in the example
549 * below. Among other things, this approach avoids problems that can be caused
550 * by spurious wakeups.
551 *
552 * {@snippet lang=java :
553 * synchronized (obj) {
554 * while ( <condition does not hold and timeout not exceeded> ) {
555 * long timeoutMillis = ... ; // recompute timeout values
556 * int nanos = ... ;
557 * obj.wait(timeoutMillis, nanos);
558 * }
559 * ... // Perform action appropriate to condition or timeout
560 * }
561 * }
562 *
563 * @param timeoutMillis the maximum time to wait, in milliseconds
564 * @param nanos additional time, in nanoseconds, in the range 0-999999 inclusive
565 * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
575 * @see #wait(long)
576 */
577 public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
578 if (timeoutMillis < 0) {
579 throw new IllegalArgumentException("timeoutMillis value is negative");
580 }
581
582 if (nanos < 0 || nanos > 999999) {
583 throw new IllegalArgumentException(
584 "nanosecond timeout value out of range");
585 }
586
587 if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
588 timeoutMillis++;
589 }
590
591 wait(timeoutMillis);
592 }
593
594 /**
595 * Called by the garbage collector on an identity object when garbage collection
596 * determines that there are no more references to the object.
597 * An identity class may override the {@code finalize} method to dispose of
598 * system resources or to perform other cleanup.
599 * <div class="preview-block">
600 * <div class="preview-comment">
601 * The {@code finalize} method of a value class is never directly
602 * invoked by the garbage collector. This includes the case where an
603 * abstract value class declares a {@code finalize} method and the
604 * class is extended by an identity class; the garbage collector never
605 * directly invokes the {@code finalize} method declared by the
606 * abstract value class.
607 * </div>
608 * </div>
609 * <p>
610 * <b>When running in a Java virtual machine in which finalization has been
611 * disabled or removed, the garbage collector will never call {@code finalize()}
612 * for any object. In a Java virtual machine in which finalization is
613 * enabled, the garbage collector might call {@code finalize} only after an
614 * indefinite delay.</b>
615 * <p>
616 * The general contract of {@code finalize} is that it is invoked
617 * if and when the Java virtual
618 * machine has determined that there is no longer any
619 * means by which this object can be accessed by any thread that has
620 * not yet died, except as a result of an action taken by the
621 * finalization of some other object or class which is ready to be
622 * finalized. The {@code finalize} method may take any action, including
623 * making this object available again to other threads; the usual purpose
624 * of {@code finalize}, however, is to perform cleanup actions before
625 * the object is irrevocably discarded. For example, the finalize method
626 * for an object that represents an input/output connection might perform
627 * explicit I/O transactions to break the connection before the object is
628 * permanently discarded.
629 * <p>
630 * The {@code finalize} method of class {@code Object} performs no
631 * special action; it simply returns normally. Subclasses of
632 * {@code Object} may override this definition.
|