简体   繁体   English

如何在Java中使用wait()/ notify()

[英]How to use wait()/notify() in Java

I know that there are a few threads open regarding this topic, but I'm just looking for a VERY ELEMENTARY example of how to use wait() and notify() in Java. 我知道有关于这个主题的一些线程是开放的,但我只是在寻找一个如何在Java中使用wait()和notify()的非常简单的例子。 By "VERY ELEMENTARY," I mean simply printing something out. 通过“非常元素”,我的意思是简单地打印出来。 Thanks. 谢谢。

EDIT: Here's what I have tried thus far and I get an IllegalMonitorStateException: 编辑:这是我到目前为止尝试过的,我得到一个IllegalMonitorStateException:

 public void waiting() { for(int i = 0; i < 10; i++) { if(i == 5) try { this.wait(); } catch (InterruptedException e) { } else System.out.println(i); } System.out.println("notify me now"); this.notify(); } 

wait and notify are used in synchronized block while using threads to suspend and resume where left off. 等待和通知在同步块中使用,同时使用线程挂起并从中断处继续。

Wait immediately looses the lock, whereas Nofity will leave the lock only when the ending bracket is encountered. 等待立即失去锁定,而Nofity仅在遇到结束括号时才会离开锁定。

public class Mythread implements Runnable{



public synchronized void goo(){

System.out.println("Before Wait");

wait();

System.out.println("After Wait");


}


public synchronized void foo(){

System.out.println("Before Notify");

notify();

System.out.println("After Notify");

}


public class Test{

public static  void main(String[] args){

Thread t = new Thread(new Mythread);

t.start();

 }
}

Your IllegalMonitorStateException is due to the fact that you must synchronize on the object before calling wait or notify. 您的IllegalMonitorStateException是由于您必须在调用wait或notify之前同步对象。 So 所以

this.wait

needs to be 需要是

   synchronized(this) {
      this.wait();
    }

Your example won't run because you'll never get to the notify call... as soon as your thread hits wait, it will suspend and advance no further. 您的示例将无法运行,因为您永远不会进入notify调用...一旦您的线程等待,它将暂停并不再前进。 For wait / notify to work, you have to have two threads. 要使wait / notify工作,您必须有两个线程。 One thread suspends when the wait method is invoked, and eventually, the second thread calls synchronized(this) { this.notify() } to cause the first thread to wake up and continue executing below the wait call. 一个线程在调用wait方法时挂起,最终,第二个线程调用synchronized(this) { this.notify() }以使第一个线程唤醒并在等待调用下继续执行。

The synchronization is required because you would ordinarily check some condition before waiting, ie, 同步是必需的,因为您通常会在等待之前检查某些条件,即

  synchronized(this) { 
      if(! this.isReady) {
         this.wait(); 
      }
   }

You need to synchronize to make sure no other thread changes the state of the isReady flag between the line where you check the variable and the line where you wait. 您需要进行同步以确保没有其他线程更改检查变量的行与您等待的行之间的isReady标志的状态。 So your notify code would 所以你的通知代码会

   synchronized(this) {
      isReady = true;
      this.notify();
   }

Now the order of the method calls doesn't matter. 现在方法调用的顺序无关紧要。 If you notify first, no thread will wake up, but that's ok, because you aren't going to sleep since isReady = true. 如果你先通知,没有线程会被唤醒,但没关系,因为你不会睡觉因为isReady = true。 If you go to sleep first, isReady = true does nothing, but the notify call wakes up the thread. 如果你先睡觉,isReady = true什么也不做,但是通知调用会唤醒线程。 Finally, the synchronization ensures that you don't check the variable in thread A, then have thread B set the variable and notify (doing nothing), then have thread A go to sleep and never wake up. 最后,同步确保您不检查线程A中的变量,然后让线程B设置变量并通知(什么也不做),然后让线程A进入休眠状态并且永远不会唤醒。

Hope that helps. 希望有所帮助。

wait()notify()用于同步线程:一个线程可以告诉wait() ,并且在收到notify()调用之前不会继续执行任何操作。

这些函数的基本思想是wait()挂起一个线程(让它进入休眠状态),而notify()使线程在它进入休眠状态时从它离开的地方获取。

Take a look at: this or just look up simple prodcuer consumer problem java on google. 看一下: 这个或者只是在google上查找simple prodcuer consumer problem java I am sure you will find something to suit your needs. 我相信你会找到适合你需求的东西。

在oracle java站点的受保护块上查看此示例 - 它包含一个简单的生产者 - 消费者问题的工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM