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 *
36 * @see java.lang.Class
37 * @since 1.0
38 */
39 public class Object {
40
41 /**
42 * Constructs a new object.
43 */
44 @IntrinsicCandidate
45 public Object() {}
46
47 /**
48 * Returns the runtime class of this {@code Object}. The returned
49 * {@code Class} object is the object that is locked by {@code
50 * static synchronized} methods of the represented class.
51 *
52 * <p><b>The actual result type is {@code Class<? extends |X|>}
53 * where {@code |X|} is the erasure of the static type of the
54 * expression on which {@code getClass} is called.</b> For
279 * <p>
280 * The awakened thread will not be able to proceed until the current
281 * thread relinquishes the lock on this object. The awakened thread will
282 * compete in the usual manner with any other threads that might be
283 * actively competing to synchronize on this object; for example, the
284 * awakened thread enjoys no reliable privilege or disadvantage in being
285 * the next thread to lock this object.
286 * <p>
287 * This method should only be called by a thread that is the owner
288 * of this object's monitor. A thread becomes the owner of the
289 * object's monitor in one of three ways:
290 * <ul>
291 * <li>By executing a synchronized instance method of that object.
292 * <li>By executing the body of a {@code synchronized} statement
293 * that synchronizes on the object.
294 * <li>For objects of type {@code Class,} by executing a
295 * static synchronized method of that class.
296 * </ul>
297 * <p>
298 * Only one thread at a time can own an object's monitor.
299 *
300 * @throws IllegalMonitorStateException if the current thread is not
301 * the owner of this object's monitor.
302 * @see java.lang.Object#notifyAll()
303 * @see java.lang.Object#wait()
304 */
305 @IntrinsicCandidate
306 public final native void notify();
307
308 /**
309 * Wakes up all threads that are waiting on this object's monitor. A
310 * thread waits on an object's monitor by calling one of the
311 * {@code wait} methods.
312 * <p>
313 * The awakened threads will not be able to proceed until the current
314 * thread relinquishes the lock on this object. The awakened threads
315 * will compete in the usual manner with any other threads that might
316 * be actively competing to synchronize on this object; for example,
317 * the awakened threads enjoy no reliable privilege or disadvantage in
318 * being the next thread to lock this object.
319 * <p>
320 * This method should only be called by a thread that is the owner
321 * of this object's monitor. See the {@code notify} method for a
322 * description of the ways in which a thread can become the owner of
323 * a monitor.
324 *
325 * @throws IllegalMonitorStateException if the current thread is not
326 * the owner of this object's monitor.
327 * @see java.lang.Object#notify()
328 * @see java.lang.Object#wait()
329 */
330 @IntrinsicCandidate
331 public final native void notifyAll();
332
333 /**
334 * Causes the current thread to wait until it is awakened, typically
335 * by being <em>notified</em> or <em>interrupted</em>.
336 * <p>
337 * In all respects, this method behaves as if {@code wait(0L, 0)}
338 * had been called. See the specification of the {@link #wait(long, int)} method
339 * for details.
340 *
341 * @throws IllegalMonitorStateException if the current thread is not
342 * the owner of the object's monitor
343 * @throws InterruptedException if any thread interrupted the current thread before or
344 * while the current thread was waiting. The <em>interrupted status</em> of the
345 * current thread is cleared when this exception is thrown.
346 * @see #notify()
347 * @see #notifyAll()
348 * @see #wait(long)
349 * @see #wait(long, int)
350 */
351 public final void wait() throws InterruptedException {
352 wait(0L);
353 }
354
355 /**
356 * Causes the current thread to wait until it is awakened, typically
357 * by being <em>notified</em> or <em>interrupted</em>, or until a
358 * certain amount of real time has elapsed.
359 * <p>
360 * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
361 * had been called. See the specification of the {@link #wait(long, int)} method
362 * for details.
363 *
364 * @param timeoutMillis the maximum time to wait, in milliseconds
365 * @throws IllegalArgumentException if {@code timeoutMillis} is negative
366 * @throws IllegalMonitorStateException if the current thread is not
367 * the owner of the object's monitor
368 * @throws InterruptedException if any thread interrupted the current thread before or
369 * while the current thread was waiting. The <em>interrupted status</em> of the
370 * current thread is cleared when this exception is thrown.
371 * @see #notify()
372 * @see #notifyAll()
373 * @see #wait()
374 * @see #wait(long, int)
375 */
376 public final void wait(long timeoutMillis) throws InterruptedException {
377 if (!Thread.currentThread().isVirtual()) {
378 wait0(timeoutMillis);
379 return;
380 }
381
382 // virtual thread waiting
383 boolean attempted = Blocker.begin();
384 try {
385 wait0(timeoutMillis);
386 } catch (InterruptedException e) {
387 // virtual thread's interrupt status needs to be cleared
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,
481 * or if the value of {@code nanos} is out of range
482 * @throws IllegalMonitorStateException if the current thread is not
483 * the owner of the object's monitor
484 * @throws InterruptedException if any thread interrupted the current thread before or
485 * while the current thread was waiting. The <em>interrupted status</em> of the
486 * current thread is cleared when this exception is thrown.
487 * @see #notify()
488 * @see #notifyAll()
489 * @see #wait()
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++;
|
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
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
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++;
|