@Rollerball is right. The wait()
is called, so that the thread can wait for some condition to occur when this wait()
call happens, the thread is forced to give up its lock.
To give up something, you need to own it first. Thread needs to own the lock first.
Hence the need to call it inside a synchronized
method/block.
Yes, I do agree with all the above answers regarding the potential damages/inconsistencies if you did not check the condition within synchronized
method/block. However as @shrini1000 has pointed out, just calling wait()
within synchronized block will not avert this inconsistency from happening.