为什么在 Java 的 Object 类中声明了 wait() 和 notify()? [英] Why are wait() and notify() declared in Java's Object class?

查看:43
本文介绍了为什么在 Java 的 Object 类中声明了 wait() 和 notify()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 wait()notify() 方法声明在 Object 类中,而不是 Thread代码>类?

Why are the wait() and notify() methods declared in the Object class, rather than the Thread class?

推荐答案

因为,您要等待给定的对象(或者特别是它的监视器)使用此功能.

Because, you wait on a given Object (or specifically, its monitor) to use this functionality.

我认为您可能误解了这些方法的工作原理.它们不是简单地处于线程粒度级别,即 不是 只是调用 wait() 并被下一次调用 唤醒的情况通知().相反,您总是在特定对象上调用 wait(),并且只会通过调用该对象上的 notify 来唤醒.

I think you may be mistaken on how these methods work. They're not simply at a Thread-granularity level, i.e. it is not a case of just calling wait() and being woken up by the next call to notify(). Rather, you always call wait() on a specific object, and will only be woken by calls to notify on that object.

这很好,否则并发原语将无法扩展;这相当于拥有全局命名空间,因为在程序中任何地方对 notify() 的任何调用都有可能弄乱任何并发代码,因为它们会唤醒任何线程阻塞在 wait() 调用上.这就是您在特定对象上调用它们的原因;它为等待通知对提供了一个操作上下文,因此当您调用 myBlockingObject.notify() 时,在私有对象上,您可以确保您只会唤醒调用的线程类中的等待方法.一些可能正在等待另一个对象的 Spring 线程不会被这个调用唤醒,反之亦然.

This is good because otherwise concurrency primitives just wouldn't scale; it would be equivalent to having global namespaces, since any calls to notify() anywhere in your program would have the potential to mess up any concurrent code as they would wake up any threads blocking on a wait() call. Hence the reason that you call them on a specific object; it gives a context for the wait-notify pair to operate on, so when you call myBlockingObject.notify(), on a private object, you can be sure that you'll only wake up threads that called wait methods in your class. Some Spring thread that might be waiting on another object will not be woken up by this call, and vice versa.

或者从另一个角度解决它 - 我希望从你的问题中你认为你会得到等待线程的句柄并在那个线程notify()> 唤醒它.不这样做的原因是你必须自己做很多家务.将要等待的线程必须在其他线程可以看到的地方发布对自身的引用;这必须正确同步,以确保一致性和可见性.当你想唤醒一个线程时,你必须抓住这个引用,唤醒它,然后从你读取它的任何地方删除它.与仅在睡眠线程中调用 myObj.wait() 然后在 唤醒线程中的 myObj.notify().

Or to address it from another perspective - I expect from your question you thought you would get a handle to the waiting thread and call notify() on that Thread to wake it up. The reason it's not done this way, is that you would have to do a lot of housekeeping yourself. The thread going to wait would have to publish a reference to itself somewhere that other threads could see it; this would have to be properly synchronized to enforce consistency and visibility. And when you want to wake up a thread you'd have to get hold of this reference, awaken it, and remove it from wherever you read it from. There's a lot more manual scaffolding involved, and a lot more chance of going wrong with it (especially in a concurrent environment) compared to just calling myObj.wait() in the sleeping thread and then myObj.notify() in the waker thread.

这篇关于为什么在 Java 的 Object 类中声明了 wait() 和 notify()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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