Lets say t1 enters the waitForCondition()
method. So t1 now has this
as a lock. Meanwhile, some other thread has just acquired lock
object and is trying call waitForContion()
.
t2 holds
lock
but is waiting forthis
to enterwaitForContion()
.t1 holds
this
but is waiting forlock
to exitwaitForContion()
.
That is a deadlock. Neither of them can make any progress and are waiting on each other.
To avoid this, one strategy is to make sure any thread has all the resources it needs to complete. Here it means that lock
and this
can only be acquired together and not otherwise.
Also, when lock.wait()
is called, only lock
is released while this
is not. So, in such a case no thread can call waitForContion()
on that
object.