您的位置 首页 java

Java 并发中,wait()的注释说明

Java 并发中,wait()的注释说明

Java 并发中,wait()的注释说明

为什么 wait()的时候必须释放锁

wait()方法的注释

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

导致当前线程等待,直到另一个线程调用此对象的 notify()方法或 notifyAll()方法。

In other words, this method behaves exactly as if it simply performs the call wait(0).

换句话说,这个方法的行为与简单执行调用 wait(0)的行为完全一样。

     public final void wait() throws InterruptedException {
        wait(0);
    }
  

The current thread must own this object’s monitor.

当前线程必须拥有这个对象的 monitor。

The thread releases ownership of this monitor

该线程释放该监视器的所有权

and waits

并等待,

until another thread notifies threads waiting on this object’s monitor to wake up either through a call to the notify method or the notifyAll method.

直到另一个线程通过调用 notify 方法或 notifyAll 方法来通知在该对象的 monitor 上等待的线程醒来。

然后,该线程等待,直到它可以重新获得 monitor 的所有权并恢复执行。

As in the one argument version,

和单参数版本一样,

interrupts and spurious wakeups are possible, and this method should always be used in a loop:

中断和假性唤醒是可能的,这个方法应该总是在一个循环中使用:

  synchronized  (obj) {
               while (<condition does not hold>)
                   obj.wait();// 如果不符合条件时,在循环中使用 wait() 
               ... // Perform action appropriate to condition  // 执行适合条件的动作
           }
  

This method should only be called by a thread that is the owner of this object’s monitor.

这个方法只能由作为这个对象的 monitor 的所有者的线程来调用。

See the notify method for a description of the ways in which a thread can become the owner of a monitor.

关于一个线程成为 monitor 所有者的方式,请看 notify 方法的描述。

抛出异常

IllegalMonitorStateException – if the current thread is not the owner of the object’s monitor.

如果当前线程不是该对象的监视器的所有者。抛出 IllegalMonitorStateException。

Interrupted Exception – if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

如果任何线程在当前线程等待通知之前或期间打断了当前线程。当这个异常被抛出时,当前线程的中断状态被清除。抛出 InterruptedException。

See Also:notify(), notifyAll()

也请参见。notify(), notifyAll()。

文章来源:智云一二三科技

文章标题:Java 并发中,wait()的注释说明

文章地址:https://www.zhihuclub.com/194015.shtml

关于作者: 智云科技

热门文章

网站地图