< prev index next >

src/java.base/share/classes/java/lang/Object.java

Print this page

  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  *
 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

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
388             Thread.currentThread().getAndClearInterrupt();
389             throw e;
390         } finally {
391             Blocker.end(attempted);
392         }
393     }
394 
395     // final modifier so method not in vtable
396     private final native void wait0(long timeoutMillis) throws InterruptedException;
397 
398     /**
399      * Causes the current thread to wait until it is awakened, typically
400      * by being <em>notified</em> or <em>interrupted</em>, or until a
401      * certain amount of real time has elapsed.
402      * <p>
403      * The current thread must own this object's monitor lock. See the
404      * {@link #notify notify} method for a description of the ways in which
405      * a thread can become the owner of a monitor lock.
406      * <p>
407      * This method causes the current thread (referred to here as <var>T</var>) to
408      * place itself in the wait set for this object and then to relinquish any
409      * and all synchronization claims on this object. Note that only the locks
410      * on this object are relinquished; any other objects on which the current
411      * thread may be synchronized remain locked while the thread waits.

  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.IntrinsicCandidate;
 29 
 30 /**
 31  * Class {@code Object} is the root of the class hierarchy.
 32  * Every class has {@code Object} as a superclass. All objects,
 33  * including arrays, implement the methods of this class.
 34  *
 35  * @see     java.lang.Class
 36  * @since   1.0
 37  */
 38 public class Object {
 39 
 40     /**
 41      * Constructs a new object.
 42      */
 43     @IntrinsicCandidate
 44     public Object() {}
 45 
 46     /**
 47      * Returns the runtime class of this {@code Object}. The returned

356      * by being <em>notified</em> or <em>interrupted</em>, or until a
357      * certain amount of real time has elapsed.
358      * <p>
359      * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
360      * had been called. See the specification of the {@link #wait(long, int)} method
361      * for details.
362      *
363      * @param  timeoutMillis the maximum time to wait, in milliseconds
364      * @throws IllegalArgumentException if {@code timeoutMillis} is negative
365      * @throws IllegalMonitorStateException if the current thread is not
366      *         the owner of the object's monitor
367      * @throws InterruptedException if any thread interrupted the current thread before or
368      *         while the current thread was waiting. The <em>interrupted status</em> of the
369      *         current thread is cleared when this exception is thrown.
370      * @see    #notify()
371      * @see    #notifyAll()
372      * @see    #wait()
373      * @see    #wait(long, int)
374      */
375     public final void wait(long timeoutMillis) throws InterruptedException {
376         if (timeoutMillis < 0) {
377             throw new IllegalArgumentException("timeout value is negative");

378         }
379 
380         if (Thread.currentThread() instanceof VirtualThread vthread) {
381             try {
382                 wait0(timeoutMillis);
383             } catch (InterruptedException e) {
384                 // virtual thread's interrupt status needs to be cleared
385                 vthread.getAndClearInterrupt();
386                 throw e;
387             }
388         } else {
389             wait0(timeoutMillis);






390         }
391     }
392 
393     // final modifier so method not in vtable
394     private final native void wait0(long timeoutMillis) throws InterruptedException;
395 
396     /**
397      * Causes the current thread to wait until it is awakened, typically
398      * by being <em>notified</em> or <em>interrupted</em>, or until a
399      * certain amount of real time has elapsed.
400      * <p>
401      * The current thread must own this object's monitor lock. See the
402      * {@link #notify notify} method for a description of the ways in which
403      * a thread can become the owner of a monitor lock.
404      * <p>
405      * This method causes the current thread (referred to here as <var>T</var>) to
406      * place itself in the wait set for this object and then to relinquish any
407      * and all synchronization claims on this object. Note that only the locks
408      * on this object are relinquished; any other objects on which the current
409      * thread may be synchronized remain locked while the thread waits.
< prev index next >