Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between wait() and this.wait()

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 !

like image 262
TheDude Avatar asked Mar 18 '23 23:03

TheDude


1 Answers

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.

like image 147
Cacho Santa Avatar answered Mar 31 '23 10:03

Cacho Santa