Wednesday, July 22, 2015

Why wait(), notify() and notifyAll() are defined in Object rather than Thread

Locks are acquired on the object (monitor). One thread does not know which other thread has acquired the lock. it only knows that the monitor has been acquired and thus it has to wait.


In the above banking example, there are many ways (channels) to access any bank account.

If in a joint-account one person tries debit and other online, the person who comes first gets the money while the other has to wait.

In this example, the lock is acquired on the account and not on the channel.

Hence, wait(), notify() and notifyAll() are defined in Object rather than Thread.

No comments:

Post a Comment

Home