简体   繁体   English

等等 - 通知工作?

[英]Wait - Notify working?

This is the code I have written, to test the working of wait() and notify() . 这是我编写的代码,用于测试wait()notify() Now I have a bunch of doubts. 现在我有一堆疑惑。

class A extends Thread {
  public void run() {
    try {
      wait();
      for (int i = 1; i <= 5; i++) {
        System.out.println(i);
        sleep(500);
      }
    } catch (Exception e) {
    }
    System.out.println("End Of Thread");
  }
}

class ThreadWaitNotify {
  public static void main(String args[]) {
    try {
      A t = new A();
      t.start();
      t.wait();
      t.notify();
      t.join();
      System.out.println("End Of Main");
    } catch (Exception e) {
    }
  }
}

My questions are: 我的问题是:

  1. When I write t.wait() in main, the main does not execute further, and I am not able to resume it further. 当我在main中写入t.wait()时,main不会进一步执行,我无法继续进行。 How to do that? 怎么做?
  2. Secondly I have written wait() in thread also, due to which it only prints "End Of Thread", not the loop? 其次我也在线程中写了wait() ,因为它只打印“End Of Thread”,而不是循环? Even if I notify() from main or not... 即使我从主要或不notify() ...
  3. Now if I write notify() in main it does not complete execution. 现在,如果我在main中编写notify() ,它就不会完成执行。 While on commenting that line it finishes execution and prints "End Of Main". 在评论该行时,它完成执行并打印“End Of Main”。

My questions are: 我的问题是:

When I write t.wait() in main, the main does not execute further, and I am not able to resume it further. 当我在main中写入t.wait()时,main不再执行,我无法继续进行。 How to do that? 怎么做?

The thread that is running main does not hold the lock on t when it calls t.wait() . 正在运行main的线程在调用t.wait()时不会锁定t As you'll see from the JavaDoc for wait() : 正如您将从JavaDoc中看到wait()

Throws : IllegalMonitorStateException - if the current thread is not the owner of the object's monitor. 抛出 :IllegalMonitorStateException - 如果当前线程不是对象监视器的所有者。

So your call to wait() is resulting in the IllegalMonitorStateException being thrown. 因此,调用wait()会导致抛出IllegalMonitorStateException。 Your empty catch block simply discards the error, which makes it difficult to debug. 空的catch块只是丢弃错误,这使得调试变得困难。 You can show the error using ex.printStackTrace() ; 您可以使用ex.printStackTrace()显示错误; or you can re-throw it as an unchecked exception: 或者您可以将其作为未经检查的异常重新抛出:

    throw new RuntimeException(ex);

To fix the wait() call, you'd need to synchronize on t: 要修复wait()调用,您需要在t上进行同步:

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

You'll also need to hold the same lock when you call notify(). 调用notify()时,您还需要保持相同的锁定。

Secondly I have written wait() in thread also, due to which it only prints "End Of Thread", not the loop? 其次我也在线程中写了wait(),因为它只打印“End Of Thread”,而不是循环? Even if I notify() from main or not... 即使我从主要或不通知()...

Same thing is happening here, but in a different thread. 同样的事情发生在这里,但在另一个线程。 The new thread that you create doesn't own the lock on t, so the wait() call throws an Exception. 您创建的新线程不拥有t上的锁,因此wait()调用将抛出异常。 Again, you're discarding the exception, by not handling it correctly. 同样,您正在通过不正确处理它来丢弃异常。

Now if I write notify() in main it does not complete execution. 现在,如果我在main中编写notify(),它就不会完成执行。 While on commenting that line it finishes execution and prints "End Of Main". 在评论该行时,它完成执行并打印“End Of Main”。

I assume you mean "commenting out the calls to wait() and notify()". 我假设你的意思是“注释掉wait()和notify()的调用”。 This is expected. 这是预料之中的。 The run() method has finished, that is: it caught the IllegalMonitorStateException that was thrown and carried on to the end of the method. run()方法已经完成,即:它捕获了抛出的IllegalMonitorStateException并将其继续到方法的末尾。 The join() method returns when the thread has finished executing. 当线程完成执行时,join()方法返回。 This happens almost immediately, because the wait() call throws an exception immediately. 这几乎立即发生,因为wait()调用立即抛出异常。

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

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