gpt4 book ai didi

java - wait() 和 this.wait() 的区别

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:45:58 26 4
gpt4 key购买 nike

我在这里得到了一个 java 代码片段,这让我想知道调用 wait()this.wait() 之间的区别是什么。

假设您有一个类,该类具有获取资源的方法并且是同步的。通常,如果资源不可用,我会在 while 循环中编写 wait(),但是如果调用 this.wait() 会发生什么?谁的显示器会被屏蔽?类对象或资源的监视器?语句"this" 指的是哪个对象?

很抱歉问了这个 super 基本的问题,但我无法通过谷歌找到任何帮助。

感谢您的帮助!

最佳答案

在那种情况下没有任何区别,this.wait()wait() 将调用相同的方法。

看看神谕docs关于 Java 中的 this 关键字。

来自文档:

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

关于java - wait() 和 this.wait() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com