Sunday 17 May 2015

How Blocked thread and Wait thread wakes up ?


Scenario.

Thread A goes in side a Synchronized method hence acquires the Lock of that OBJECT.
While the Thread A is inside Syc Block, Two more Threads (Thread B and C) tries to execute that Sync block, Since lock is acquired from Thread A, subsequent Thread B and C are in BLOCKED state.

Now Assume, some condition inside the Sync Block has not meet and then Thread A goes to Wait state, and thus relinquishing the LOCK of Object.

ex :

    public synchronized void getLock()  {
            while (!isLockAvailable) {
                wait();
             //Do some task
             sleep(10000);
             isLockAvailable=true
            }
           
            //Set the flag so that next thread waits in the while loop
            isLockAvailable = false;

    }

Now .....
TO be completed 

No comments:

Post a Comment