Though there are lots of articles and SO posts on this topic , still i have some doubts. So please help me understand. Say i write :
1.Class A(){}
2.public static void main(String[] s){
3. A obj= new A();
4. synchronized(obj){
5. while(!condition)
6. obj.wait();
7. }
8.}
Now according to explanations ,if we don't use synchronized block, Thread woken-up from sleep may loose notifications. BUT line 6 released the lock on obj i.e. its monitor is owned by another thread. Now when that thread invoked notify() , how does this thread get notified since obj's monitor is not owned by this thread. Moreover , line 4 code executes only once ,NOT on each wake-up event of this thread. So what EXACTLY is NEED of synchronized before wait()?
Edit: "line 4 code executes only once" wrong assumption. Threads in synchronized blocks reacquire lock after they are resumed from sleep as mentioned in answer.Thanks
waitwill throw an exception to start with... (Andwaitonly returns after reacquiring the monitor.)