Originally posted by Lovleen Gupta:
...Here the run() method is synchronizing the code on this. So, does it mean, it is synchronizing on t1 object? ...
Yes, "this" refers to the current instance, and t1 references the only instance created.
Originally posted by Lovleen Gupta:
...Also, then it calls wait(); and since there is no notify(), it will keep on waiting indefinitely. What changes do we need to make in the program to make it print "Notified" also? ...
Remember, each instance has an object lock, but there is
also a separate class lock. Your waiting thread is waiting to re-acquire the object lock. So instead of synchronizing the code in startme on the
class (using the class lock), you could synchronize on the object (this). Then the thread waiting for the object lock will be notified.
Another way to get "Notified" to print would be to simply add a "time-out" parameter to the wait call. For example, if you call wait(5000), then the thread will stop waiting after 5 seconds.