//########
//public final void wait() throws InterruptedException
//########
/**
* Causes the current thread to wait until another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object.
* 促使此当前线程进入此object的等待队列,直到其他线程执行 此 object 的 notify() 或 notifyAll()
* 才会离开此对象的等待队列 获取CPU并竞争此object锁
* In other words, this method behaves exactly as if it simply
* performs the call {@code wait(0)}.
* 等同于 简单执行 wait(0) 这个行为
* <p>
* The current thread must own this object's monitor. The thread
* 执行wait的线程注定必须拥有此对象监视器的所有权,拥有的大前提如下:
* releases ownership of this monitor and waits until another thread
* '某线程释放了对象监控的所有权' 且 直到另外一个线程通知 '正在等待此对象监视器的线程们'从等待队列醒来
* (备注:醒来后的线程会等待CPU时间片走正常的竞争逻辑代码去争夺对象监视器)
* notifies threads waiting on this object's monitor to wake up
* 通知 '正在等待此对象监视器的线程们'从等待队列醒来 的方法 有 notify 与 notifyAll
* either through a call to the {@code notify} method or the
* {@code notifyAll} method. The thread then waits until it can
* '正在等待此对象监视器的线程们' 只有等到 成为 对象监视器的 拥有者 时 才能 恢复执行
* (备注:所谓恢复执行即,在哪段代码失去的CPU,就在哪段代码获取CPU和拥有对象监视器 并继续往下执行下面的代码)
* re-obtain ownership of the monitor and resumes execution.
*
* <p>
* As in the one argument version, interrupts and spurious wakeups are
* 在一个参数版本中 , 线程 有 中断 和 虚假唤醒 的可能
* possible, and this method should always be used in a loop:
* 且 此方法应该始终在一个循环中使用
* <pre>
* 同步代码块 约束 线程们对这段代码 同步串行 执行
* synchronized (obj) {
* while (<condition does not hold>)
* obj.wait();
* ... // Perform action appropriate to condition
* }
* </pre>
* This method should only be called by a thread that is the owner
* 这个object的wait方法只能被 '拥有这个对象监视器' 的线程执行
* of this object's monitor. See the {@code notify} method for a
*
* description of the ways in which a thread can become the owner of
* notify 这个方法 描述了 一个线程 成为 object对象的monitor监视器 的 owner拥有者 的 实现细节
* a monitor.
*
* @throws IllegalMonitorStateException if the current thread is not
* the owner of the object's monitor.
* 如果 此当前线程 不是 该对象监视器 的 拥有者时 则 抛出 IllegalMonitorStateException 型异常
* @throws InterruptedException if any thread interrupted the
* current thread before or while the current thread
* was waiting for a notification. The <i>interrupted
* status</i> of the current thread is cleared when
* this exception is thrown.
* 如果 其他任意一个线程 对 这个当前线程发出中断信号
* 备注: (这个收到中断信号的当前线程有可能正在该对象监视器的等待队列中等着某个线程调用对象的notify或notifyAll)
* 当此异常被抛出时 这个线程的中断状态会被重置(即:中断信号标识会重置为非中断)
* #对于线程中断标识的一点科普
* a线程失去CPU进入资源等待队列中,b线程拥有CPU并对a线程发送了一个中断信号(此时a线程因失去cpu不能及时对这个中断信号及时作出响应,所以b线程会设置a线程的中断信号标识字段为true),
* a线程重获CPU后先检查自身的中断信号信号字段的值以确认自己失去CPU期间是否有其他线程中断过自己,
* 如果有那么就抛出中断异常 InterruptedException 以响应 这个中断信号
* 如果没有那么就在失去CPU的代码段处重获CPU并继续执行代码
* @see java.lang.Object#notify()
* @see java.lang.Object#notifyAll()
*/
//########
//public final native void notify();
//########
/**
* Wakes up a single thread that is waiting on this object's
* 唤醒 正在等待此对象监视器 的 单个线程
* monitor. If any threads are waiting on this object, one of them
* 如果有大于等于1个线程正在等待这个对象, 那么他们中的一个会被选择进行唤醒
* is chosen to be awakened. The choice is arbitrary and occurs at
* 选择的机制是任意的 且 选择的实现也是可自行决定
* the discretion of the implementation. A thread waits on an object's
* 一个线程调用 wait() 方法在一个对象的监视器 上 进行 等待
* monitor by calling one of the {@code wait} methods.
* <p>
* The awakened thread will not be able to proceed until the current
* 如果占据锁的当前线程没有释放这个对象上的锁,那么被唤醒的线程依然无法继续执行(即会继续等待)
* thread relinquishes the lock on this object. The awakened thread will
* 被唤醒的线程 使用正常方式 与 '其他活跃的线程' 以同步竞争的方式 , 竞争 这个对象的锁(或者说 锁定独占此对象)
* compete in the usual manner with any other threads that might be
*
* actively competing to synchronize on this object; for example, the
* 举例解释: 被唤醒的进程 在将来成为下一个锁定此对象的进程的过程中 是没有 优势与劣势
* awakened thread enjoys no reliable privilege or disadvantage in being
*
* the next thread to lock this object.
* <p>
* This method should only be called by a thread that is the owner
* 此方法只会被 '此对象监视器的拥有者(即成功锁定此对象的那个线程)' 调用
* of this object's monitor. A thread becomes the owner of the
* 一个线程 想成为 此对象监视器的拥有者, 可通过如下的方法 来实现
* object's monitor in one of three ways:
* <ul>
* <li>By executing a synchronized instance method of that object.
* 执行此对象的一个同步实例方法
* <li>By executing the body of a {@code synchronized} statement
* 通过执行 synchronized 语句的执行体 在这个对象上实现 '多线程同步串行'
* that synchronizes on the object.
* <li>For objects of type {@code Class,} by executing a
* 对于类型为Class的objects, 通过执行该class的 同步-静态-方法
* synchronized static method of that class.
* </ul>
* <p>
* Only one thread at a time can own an object's monitor.
* 任意时刻仅有一个线程 能 成为 这个对象监控 的 拥有者
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* 如果 此当前线程 不是 该对象监视器 的 拥有者时 则 抛出 IllegalMonitorStateException 型异常
* @see java.lang.Object#notifyAll()
* @see java.lang.Object#wait()
*/
Java 的 Object 类 下 wait 与 notify 方法 javadoc 翻译
最新推荐文章于 2021-02-16 10:21:18 发布