/** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param millis * the length of time to sleep in milliseconds * * @throws IllegalArgumentException * if the value of {@code millis} is negative * * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. * 根据系统计时器和调度程序的精度和准确性,使得当前的执行线程进入睡眠状态(暂时停止继续执行)达到指定的毫秒数。在这期间该线程不会失去任何所有权。 */ publicstaticnativevoidsleep(long millis)throws InterruptedException;
/** * 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. * 导致当前线程去等待,直到其他线程触发 * java.lang.Object#notify()方法或者一个特定的时间值执 * 行过去(设置的执行时间完成) * <p> * The current thread must own this object's monitor. * 当前线程必须拥有该对象的监视器。 * <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: * 线程称为不可调度并处于休眠状态,直到一下四种状况发生: * <ul> * <li>Some other thread invokes the {@code notify} method for this * object and thread <var>T</var> happens to be arbitrarily chosen as * the thread to be awakened. * 某些其他线程触发notify方法,对于当前对象和线程恰好被任 * 意选择为要唤醒的线程。 * <li>Some other thread invokes the {@code * notifyAll} method for this * object. * 某些其他线程触发notifyAll方法针对当前对象 * <li>Some other thread {@linkplain * Thread#interrupt() interrupts} * thread <var>T</var>. * 某些其他线程触发了interrupts方法。 * <li>The specified amount of real time has * elapsed, more or less. If * {@code timeout} is zero, however, then real * time is not taken into * consideration and the thread simply waits * until notified. * 特定的时间大致过去。如果这个时间是0,则不考虑时间,线程 * 只能等待通知 * </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 {@code wait} * method was invoked. * 一旦获得了对象的控制权,所有它的同步声明将恢复原状即调用方法是的情况。 * Thread <var>T</var> then returns from the * invocation of the {@code wait} method. * 线程返回方法的调用。 * Thus, on return from the * {@code wait} method, the synchronization state of the object and of * thread {@code T} is exactly as it was when the {@code wait} method * was invoked. * 因此,从{@code wait}方法返回时,对象和线程{@code T} * 的同步状态与调用{@code 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> * (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 * {@code InterruptedException} is thrown. * 如果当前线程被任何其他线程中断则将会抛出 * InterruptedException * This exception is not * thrown until the lock status of this object has been restored as * described above. * 上述的异常在对象锁定状态恢复之前不会发生。 * * <p> * Note that the {@code wait} method, as it * places the current thread * into the wait set for this object, unlocks * only this object; * 注意wait方法被放置在当前线程对于该独享的等待集中,解锁 * 仅针对该对象 * 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} method for a * description of the ways in which a thread can * become the owner of * a monitor. *有关线程可以成为监视器所有者的方式的描述,请参见{@codenotify}方法。 * @param timeout the maximum time to wait in milliseconds. * @throws IllegalArgumentException if the value of timeout is * negative. * @throws IllegalMonitorStateException if the current thread is not * the owner of the object's monitor. * @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. * @see java.lang.Object#notify() * @see java.lang.Object#notifyAll() */ publicfinalnativevoidwait(long timeout)throws InterruptedException;