diff a/src/java.base/share/classes/java/lang/Object.java b/src/java.base/share/classes/java/lang/Object.java
--- a/src/java.base/share/classes/java/lang/Object.java
+++ b/src/java.base/share/classes/java/lang/Object.java
@@ -368,24 +368,29 @@
* @see #notifyAll()
* @see #wait()
* @see #wait(long, int)
*/
public final void wait(long timeoutMillis) throws InterruptedException {
- long comp = Blocker.begin();
+ if (!Thread.currentThread().isVirtual()) {
+ wait0(timeoutMillis);
+ return;
+ }
+
+ // virtual thread waiting
+ boolean attempted = 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);
+ Blocker.end(attempted);
}
}
- // final modifier so method not in vtable
+ // final modifier as method not in vtable
private final native void wait0(long timeoutMillis) throws InterruptedException;
/**
* Causes the current thread to wait until it is awakened, typically
* by being notified or interrupted, or until a