参考http://www.cnblogs.com/lwbqqyumidi/p/3693015.html
1
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
* is chosen to be awakened. The choice is arbitrary and occurs at
* the discretion of the implementation. A thread waits on an object's
* monitor by calling one of the {@code wait} methods.
唤醒正在等待的监视器,如果任意一个线程在等待对象,则选择其中一个唤醒,选择时任意的发生在执行的自由裁量权,
一个线程通过wait方法等待一个对象的监视器
* <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
* that synchronizes on the object.
同步代码块
* <li>For objects of type {@code Class,} by executing a
* 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.
* @see java.lang.Object#notifyAll()
* @see java.lang.Object#wait()
*/
2
public final native void notifyAll();
/**
* Wakes up all threads that are waiting on this object's monitor. A
* thread waits on an object's monitor by calling one of the
* {@code wait} methods.
唤醒当前的所有线程
其他的同notify
* <p>
#3
* The awakened threads will not be able to proceed until the current
* thread relinquishes the lock on this object. The awakened threads
* will compete in the usual manner with any other threads that might
* be actively competing to synchronize on this object; for example,
* the awakened threads enjoy 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. See the {@code notify} method for a
* description of the ways in which a thread can become the owner of
* a monitor.
*
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* @see java.lang.Object#notify()
* @see java.lang.Object#wait()
*/
3
/**
* 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.
使当前线程等待直到其他的线程调用notify()和notifyAll(),或者指定时间已过
* <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的方法给这个object,并且线程恰巧被选择为唤醒的
* <li>Some other thread invokes the {@code notifyAll} method for this
* object.
notify的方法
* <li>Some other thread {@linkplain Thread#interrupt() interrupts}
* thread <var>T</var>.
Interrupt方法
* <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.
超过等待时间
* </ul>
* The thread <var>T</var> is then removed from the wait set for this
* object and re-enabled for thread scheduling.
The thread T 从waitset中移除然后重新调度
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.
然后从wait中返回,也就是说调用完之后和调用之前的线程thread完全相同
* <p>
* A thread can also wake up without being notified, interrupted, or
* timing out, a so-called <i>spurious wakeup</i>.
线程也可以不通过notify,Interrupt,timing out 唤醒,所以被称作是虚假唤醒
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. 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; 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.
*
* @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()
*/
网友评论