谁和什么时候调用thread.join()时通知thread.wait()? [英] who and when notify the thread.wait() when thread.join() is called?

查看:183
本文介绍了谁和什么时候调用thread.join()时通知thread.wait()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

thread.join()将调用 thread.wait(),但是谁和何时通知(或者是 thread.notify() notifyAll() thread.wait()

thread.join() will call thread.wait(), but who and when notifies (either with thread.notify() or notifyAll()) the thread.wait()?

众所周知,线程连接将等待线程完成,但是谁调用了通知呢?

As we know, thread join will wait for the thread to be completed, but who calls notify on it?

推荐答案

编辑:

哦,你说的是在里面 Thread 对象本身。在 join()里面,我们看到 wait()。类似于:

Oh, you are talking about inside of the Thread object itself. Inside of join() we do see a wait(). Something like:

while (isAlive()) {
    wait(0);
}

notify()这是由 Thread 子系统处理的。 run()方法完成后,线程<上调用 notify() / code>对象。我不确定是否可以看到实际调用 notify()的代码 - 它似乎是在本机代码中完成的。

The notify() for this is handled by the Thread subsystem. When the run() method finishes, the notify() is called on the Thread object. I'm not sure if the code that actually calls notify() can be seen -- it seems to be done in native code.

没有用户代码需要在线程上调用 notify() 对象。 Java Thread 代码在内部处理此问题。线程完成后, join()调用将返回。

No user code needs to call notify() on that Thread object. The Java Thread code handles this internally. Once the thread finishes, the join() call will return.

例如,以下代码将执行正常和 join()调用将返回正常,没有任何 wait() notify() 调用。

For example, the following code will execute fine and the join() call will return fine without any wait() or notify() calls.

Thread thread = new Thread(new Runnable() {
   public void run() {
      // no-op, just return immediately
   }
});
thread.start();
thread.join();

重要的是要注意,这种行为可能不应该依赖。 notify()调用是线程系统的内部调用。如果您正在等待线程完成,则应使用 join()

It is important to note that this behavior should probably not be relied upon. The notify() call is internal to the thread system. You should use join() if you are waiting for a thread to finish.

这篇关于谁和什么时候调用thread.join()时通知thread.wait()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆