< prev index next >

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

Print this page
@@ -23,10 +23,11 @@
   * questions.
   */
  
  package java.lang;
  
+ import jdk.internal.event.VirtualThreadPinnedEvent;
  import jdk.internal.misc.Blocker;
  import jdk.internal.vm.annotation.IntrinsicCandidate;
  
  /**
   * Class {@code Object} is the root of the class hierarchy.

@@ -368,20 +369,39 @@
       * @see    #notifyAll()
       * @see    #wait()
       * @see    #wait(long, int)
       */
      public final void wait(long timeoutMillis) throws InterruptedException {
+         if (!Thread.currentThread().isVirtual()) {
+             wait0(timeoutMillis);
+             return;
+         }
+ 
+         // virtual thread waiting
+         VirtualThreadPinnedEvent event;
+         try {
+             event = new VirtualThreadPinnedEvent();
+             event.begin();
+         } catch (OutOfMemoryError e) {
+             event = null;
+         }
          long comp = Blocker.begin();
          try {
              wait0(timeoutMillis);
          } catch (InterruptedException e) {
-             Thread thread = Thread.currentThread();
-             if (thread.isVirtual())
-                 thread.getAndClearInterrupt();
+             // virtual thread's interrupt status needs to be cleared
+             Thread.currentThread().getAndClearInterrupt();
              throw e;
          } finally {
              Blocker.end(comp);
+             if (event != null) {
+                 try {
+                     event.commit();
+                 } catch (OutOfMemoryError e) {
+                     // ignore
+                 }
+             }
          }
      }
  
      // final modifier so method not in vtable
      private final native void wait0(long timeoutMillis) throws InterruptedException;
< prev index next >