Object.wait()
来源:互联网 发布:非农数据 原油的影响 编辑:程序博客网 时间:2024/06/15 22:26
<span style="font-size:18px;">package JUC;/** * Causes the current thread to wait until either another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or a * specified amount of time has elapsed. * <Translate> * 引起当前线程等待阻塞直到其他线程调用 notify()方法或者notifyAll()方法,或者等待了足够长的时间 * <p> * The current thread must own this object's monitor. * <Translate> * 当前线程必须持有该对象的监视器 * <p> * This method causes the current thread (call it <var>T</var>) to * place itself in the wait set for this object and then to relinquish * any and all synchronization claims on this object. Thread <var>T</var> * becomes disabled for thread scheduling purposes and lies dormant * until one of four things happens: * <Translate> * 该方法引起当前线程 T 将自己放置在 该对象的 wait 集合中,并让出线程 T 在该对象上的所有锁。 * 线程 T 变为非就绪状态并保持睡眠状态,直到以下四种情况中的一种发生: * <ul> * <li>Some other thread invokes the <tt>notify</tt> method for this * object and thread <var>T</var> happens to be arbitrarily chosen as * the thread to be awakened. * <li>Some other thread invokes the <tt>notifyAll</tt> method for this * object. * <li>Some other thread {@linkplain Thread#interrupt() interrupts} * thread <var>T</var>. * <li>The specified amount of real time has elapsed, more or less. If * <tt>timeout</tt> is zero, however, then real time is not taken into * consideration and the thread simply waits until notified. * <Translate> * 1.其他线程调用了该对象的 notify() 方法,而且线程 T 碰巧被任意的(随机的)选择为被唤醒的线程; * 2.其他线程调用了该对象的 notifyAll() 方法; * 3.其他线程打断了(interrupt) 线程 T; * 4.线程 T 等待了几乎等于(timeout)的时间。如果 wait(timeout) timeout=0,则, * 实际等待的时间被忽略,线程 T 就简单等待着,直到被唤醒(notified)。 * </ul> * The thread <var>T</var> is then removed from the wait set for this * object and re-enabled for thread scheduling. It then competes in the * usual manner with other threads for the right to synchronize on the * object; once it has gained control of the object, all its * synchronization claims on the object are restored to the status quo * ante - that is, to the situation as of the time that the <tt>wait</tt> * method was invoked. Thread <var>T</var> then returns from the * invocation of the <tt>wait</tt> method. Thus, on return from the * <tt>wait</tt> method, the synchronization state of the object and of * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method * was invoked. * <Translate> * 以上几种情况的其中一种发生之后,线程 T 从 该对象的 wait set(等待集合)中删除,并重新变为就绪状态(enable for thread scheduling). * 其次,该线程变得和其他拥有该对象锁的线程一样按照通常的方式完成。 * 一旦,线程 T 获取到了该对象的控制权(锁),该线程上所有基于该对象(锁)的同步被重新赋予 * quoante 状态,该状态是指 wait() 方法被调用时的环境状态。 * 最后,线程 T 从 wait() 方法调用中返回;从wait()方法返回之后,该对象上的同步状态 * 以及该线程的同步状态变回和wait()方法调用之前的一模一样。 * <p> * A thread can also wake up without being notified, interrupted, or * timing out, a so-called <i>spurious wakeup</i>. While this will rarely * occur in practice, applications must guard against it by testing for * the condition that should have caused the thread to be awakened, and * continuing to wait if the condition is not satisfied. In other words, * waits should always occur in loops, like this one: * <pre> * synchronized (obj) { * while (<condition does not hold>) * obj.wait(timeout); * ... // Perform action appropriate to condition * } * </pre> *<Translate> *线程除了可以通过 notified, interrupted,timing out被唤醒还可以通过一种称为(spurious wakeup) *假装唤醒的方式被唤醒。 *这种情况在实际应用中很少出现,应用中必须通过测试线程被唤醒的环境是否满足,否则就继续等待; *换句话说,就是 wait 应该永远出现在loops中,就像下面这种; * synchronized (obj) { * while (<condition does not hold>) * obj.wait(timeout); * ... // Perform action appropriate to condition * } * * (For more information on this topic, see Section 3.2.3 in Doug Lea's * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming * Language Guide" (Addison-Wesley, 2001). * * <p>If the current thread is {@linkplain java.lang.Thread#interrupt() * interrupted} by any thread before or while it is waiting, then an * <tt>InterruptedException</tt> is thrown. This exception is not * thrown until the lock status of this object has been restored as * described above. *<Translate> *如果当前线程在 wait() 之前或者 wait() 过程中被其他线程打断(interrupted); *则,一个 InterruptedException 会被抛出 *这个异常不会抛出直到该对象的锁状态被重置为上述描述的情况。 * <p> * Note that the <tt>wait</tt> method, as it places the current thread * into the wait set for this object, unlocks only this object; any * other objects on which the current thread may be synchronized remain * locked while the thread waits. * <p> * This method should only be called by a thread that is the owner * of this object's monitor. See the <code>notify</code> method for a * description of the ways in which a thread can become the owner of * a monitor. *<Translate> *注意: wait() 方法将当前进程放置到该对象的 wait set 中,该进程只会放弃在该对象上 的锁,该进程在其他对象上的锁保持不变。 *该方法应该只被拥有该对象的监视器(monitor)调用,一个线程可以通过哪几种方法成为一个monitor的描述在 notify() 方法中。 * * @param timeout the maximum time to wait in milliseconds. * @exception IllegalArgumentException if the value of timeout is * negative. * @exception IllegalMonitorStateException if the current thread is not * the owner of the object's monitor. * @exception 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. * @see java.lang.Object#notify() * @see java.lang.Object#notifyAll() */public final native void wait(long timeout) throws InterruptedException;</span>
0 0
- Object.wait()
- Object.wait
- object.wait()与object.notify()
- Java Object.wait()方法
- Java之Object.WAIT()
- Object wait and notify
- Object wait方法
- Object.wait()与Object.notify()的用法
- Object.wait()与Object.notify()的用法
- Object的notify,wait,notifyAll
- object类 wait与notify
- object wait notify 实例一则
- 多线程Object之wait、notify
- Object.wait notify notifyAll native
- Object中wait和notify
- java中wait()、this.wait()、super.wait()、object.wait()的使用区别
- wait notify 都是Object的方法
- wait notify 都是Object的方法
- struct2自定义标签
- windows mysql 自动备份的几种方法
- windows下SecureCRT无法使用backspace(空格键)和上下左右键
- ubuntu文件系统结构
- wxPython Cookbook (Chatper1)part 1
- Object.wait()
- Linux统计某文件夹下文件、文件夹的个数
- 华为OJ(字符串加解密)
- 2014华为机试真题(2)
- BZOJ 2594 [Wc2006]水管局长数据加强版 LCT
- xutils 框架使用
- 一个类似的python消息响应机制的实现
- 从eclipse运行UiAutomator的方法,告别cmd
- normal、sysdba、sysoper登陆oracle的区别理解