public final void wait() throws InterruptedException { wait(0L);}public final void wait(long timeoutMillis) throws InterruptedException { long comp = Blocker.begin(); try { wait0(timeoutMillis); } catch (InterruptedException e) { Thread thread = Thread.currentThread(); if (thread.isVirtual()) thread.getAndClearInterrupt(); throw e; } finally { Blocker.end(comp); }}// final modifier so method not in vtableprivate final native void wait0(long timeoutMillis) throws InterruptedException;public final void wait(long timeoutMillis, int nanos) throws InterruptedException { if (timeoutMillis < 0) { throw new IllegalArgumentException("timeoutMillis value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) { timeoutMillis++; } wait(timeoutMillis);}
public static void sleep(long millis) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (currentThread() instanceof VirtualThread vthread) { long nanos = MILLISECONDS.toNanos(millis); vthread.sleepNanos(nanos); return; } if (ThreadSleepEvent.isTurnedOn()) { ThreadSleepEvent event = new ThreadSleepEvent(); try { event.time = MILLISECONDS.toNanos(millis); event.begin(); sleep0(millis); } finally { event.commit(); } } else { sleep0(millis); }}private static native void sleep0(long millis) throws InterruptedException;
await具体细节请看Java多线程:条件变量
public final void await() throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); ConditionNode node = new ConditionNode(); int savedState = enableWait(node);//加入条件队列 LockSupport.setCurrentBlocker(this); // for back-compatibility,将AQS对象设置到thread中 boolean interrupted = false, cancelled = false, rejected = false; while (!canReacquire(node)) {//如果被唤醒进入同步队列后就可以跳出循环 if (interrupted |= Thread.interrupted()) { if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0) break; // else interrupted after signal } else if ((node.status & COND) != 0) { try { if (rejected) node.block(); else ForkJoinPool.managedBlock(node);//阻塞线程,最终会调用LockSupport.park() } catch (RejectedExecutionException ex) { rejected = true; } catch (InterruptedException ie) { interrupted = true; } } else Thread.onSpinWait(); // awoke while enqueuing }//被唤醒 LockSupport.setCurrentBlocker(null); node.clearStatus();////lock.lock()方法:acquire(null, arg, false, false, false, 0L);//重新获取锁时已原来的savedState acquire(node, savedState, false, false, false, 0L);//重新获取锁,此时该节点已经进入了同步队列,有可能直接tryAcquire成功跳出循环,也可能需要两次循环修改node.status为WAITING、park。 if (interrupted) { if (cancelled) { unlinkCancelledWaiters(node); throw new InterruptedException(); } Thread.currentThread().interrupt(); }}
5 附录
public class Object { @IntrinsicCandidate public Object() {} @IntrinsicCandidate public final native Class<?> getClass();//返回类对象用于反射 @IntrinsicCandidate public native int hashCode(); public boolean equals(Object obj) { return (this == obj); } @IntrinsicCandidate protected native Object clone() throws CloneNotSupportedException; public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } @IntrinsicCandidate public final native void notify(); @IntrinsicCandidate public final native void notifyAll(); public final void wait() throws InterruptedException { wait(0L); } public final void wait(long timeoutMillis) throws InterruptedException { long comp = Blocker.begin(); try { wait0(timeoutMillis); } catch (InterruptedException e) { Thread thread = Thread.currentThread(); if (thread.isVirtual()) thread.getAndClearInterrupt(); throw e; } finally { Blocker.end(comp); } } // final modifier so method not in vtable private final native void wait0(long timeoutMillis) throws InterruptedException; public final void wait(long timeoutMillis, int nanos) throws InterruptedException { if (timeoutMillis < 0) { throw new IllegalArgumentException("timeoutMillis value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) { timeoutMillis++; } wait(timeoutMillis); } @Deprecated(since="9", forRemoval=true) protected void finalize() throws Throwable { }}