nits.kk
nits.kk

Reputation: 5316

Do code inside a synchronized block but after wait() runs in synchronized way?

I wish to know the code written inside the synchronized block but after wait() statement actually runs in synchronized way or not as during wait() lock is relinquished and once the thread is notified it again needs to take lock.

public void process(){
    synchronized(sharedObj){
        try{
             sharedObj.wait();
        }catch(InterruptedException ex){
              ex.printStackTrace();
        }
        // when wait is over do the line below runs in synchronized way. It is inside synchronized block but during call of wait(), lock was relinquished. 
        sharedObj.writeValue();
    }
}

Please help me clarifying my doubt.

Regards, Krishna

Upvotes: 1

Views: 877

Answers (3)

Gui Meira
Gui Meira

Reputation: 885

According to the Javadoc:

The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

So, yes, when the wait call returns, that thread will own the monitor, which means that it is running "in synchronized way" (no other thread will be able to execute that block of code or any other block of code that is synchronized using that same sharedObj before the current thread leaves the synchronized block).

By the way, also according to the Javadoc, wait should always be called in a loop, because the wait can be interrupted for some other reason that is not the notify you are waiting for.

For example:

public void process(){
    synchronized(sharedObj){
        while(!sharedObj.valueCanBeWritten()) {
            try{
                sharedObj.wait();
            }catch(InterruptedException ex){
              ex.printStackTrace();
            }
        }
        sharedObj.writeValue();
    }
}

Upvotes: 3

John Bollinger
John Bollinger

Reputation: 180113

It is true that for a thread to successfully invoke an object's wait() method, it must hold that object's monitor, and that it relinquishes the monitor during the execution of wait(). It is also true, however, that the thread re-acquires that monitor before returning from wait(). You don't need to do anything special to make that happen.

Perhaps you are confused about the fact that the thread that executes wait() may need to contend with others for the monitor after it stops waiting, but you do not need to worry; it happens before wait() returns. The integrity of the synchronized block remains intact: code executing within can always be assured that no other thread holds the synchronization object's monitor, including after a wait() is preformed.

Upvotes: 4

Louis Wasserman
Louis Wasserman

Reputation: 198023

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()

The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

Yes, it gets the lock back.

Upvotes: 5

Related Questions