< 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

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










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

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

374         }
375 
376         if (Thread.currentThread() instanceof VirtualThread vthread) {
377             try {
378                 wait0(timeoutMillis);
379             } catch (InterruptedException e) {
380                 // virtual thread's interrupt status needs to be cleared
381                 vthread.getAndClearInterrupt();
382                 throw e;
383             } finally {
384                 if (timeoutMillis > 0) {
385                     vthread.cancelWaitTimeout();
386                 }
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 >