I got a java code snippet here which got me wondering what the difference between the calls wait()
and this.wait()
is.
Lets say you have a class with a method to take a resource and that is synchronized. Normally I would write wait()
in the while
loop if the resource is not available but what happens if you call this.wait()
? Whose monitor will be blocked? The monitor of the class object or the resource? And to which object does the statement "this"
refer to?
Sorry for asking this super basic question but I couldn't find any help through google.
Thanks for helping !
There isn't any difference in that case, this.wait()
and wait()
will call the same method.
Take a look at the oracle docs about the this
keyword in Java.
From the documentation:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With