i have created a class called hotel in which i have set a boolean variable called conditionVar to false. inside the run method i have used two synchonized blocks, one is for wait and other is for notify .But the application is showing output as
it is not entering in the synchronized block of notify . Is this an concept of busy waiting? and how can the thread call notify?
The block starting at line 28 can never be entered by either thread.
Why? Because the block starting at line 11 is an infinite loop. Control cannot exit from that block until the conditionVar variable is set to true, which cannot happen.
Still not persuaded? Consider thread A. It can't get out of the line-11 block until some other thread sets conditionVar to true. Now consider thread B. It can't get out of the line-11 block until some other thread sets conditionVar to true. So B would have to set it to allow A to proceed, and A would have to set it for B to proceed. I suppose you could call that self-inflicted deadlock.
If you remove lines 13 and 23 from your code then it will call wait and then go on directly to call notify. That satisfies your stated goal of calling notify, but like Norman I have no idea of why you would want it to do that. I don't understand what the code is supposed to be doing.
Paul Clapham wrote:If you remove lines 13 and 23 from your code then it will call wait and then go on directly to call notify.
No, actually it won't. You still have the deadlock where both threads start out by waiting. How you "fix" that I have no idea, because that would require knowing what the code was really meant to do.