Sunday 2 March 2014

Why Wait and Notify is in Object Class

In java, we are acquiring locks on the resources. Every Object treated as a resource in java. Since Java is multi threaded, there is a possibility for resource corruption, if multiple threads are working on the same resource. There should be a mechanism, to safely update the resource. Here the monitor concept came to picture.

While performing any update on the resource, a thread must acquire a lock on the resource. Then the thread which acquires lock on the resource, update the resource, remaining all threads which want to work on this resource must wait until it finishes. Once the the thread finishes its updation with the resource, it asks the resource, to notify remaining threads to update. Same procedure like acquiring lock, releasing lock, notifying other threads repeats.

Since in java every resource considers to be an object, so that wait, notify and notifyAll kept in Object class.


Thread States                                                 Dead Lock                                                 Home

No comments:

Post a Comment