wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification in wait, notify and notifyAll methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification in wait, notify and notifyAll methods" Watch "Clarification in wait, notify and notifyAll methods" New topic
Author

Clarification in wait, notify and notifyAll methods

Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Hi,

I have a doubt in wait(),notify() and notifyAll()

we will call all of them inside the synchronized block

ex:

now when wait() executes the current thread under execution releases lock on q and enters in to wait set of the object q.
when some other thread obtains lock on q and executes notifyAll() it will notify all the threads in wait set of object q.

1. Now what happens when the thread wakes up will it enter runnable state or will it continue its execution from where
it stopped?

I think it enters runnable state as it already released the lock and cant continue from where it stopped. It should begin from scratch

2. Is there any difference in calling wait() and q.wait() ?

I think no

and when we call Thread.sleep(1000) then the current thread holds the lock and goes to blocked state and once the sleep time expires
continues from where it stopped as it already owns the lock. Is this correct?

3. is wait set used only by wait(), notify() and notifyAll() or will it be used by Sleep and any other APIs too? in other words
wait set == blocked state?

Thanks

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Hi Chandra,

When wait() is executed on an object, the executing thread will release the object monitor and then go into a waiting state. When it wakes up again, through a call to notify() or notifyAll() on the object it is waiting on by another thread, it enters blocking state. Blocking state is the state where it has to reacquire the object monitor. The thread will continue where it was when the wait() method was called, but it first blocks until the synchronized block it's in is free (there is no other thread holding the object monitor). After it has reacquired the lock, it will go to runnable state and continue where it left.

Yes, there is a difference between wait() and q.wait(). wait() is the same as this.wait(). If q is not the same as 'this', then the thread will wait on a different object.

When you call Thread.sleep(), if the current thread holds the lock it will not release it. It simply goes to waiting mode, after the specified amount of time is passed, and it will just continue to run without having to reacquire the lock.

What do you mean by wait set?
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Stephan wrote:
after the specified amount of time is passed, and it will just continue to run

Hello Stephan...If havent forgotten what I studied about 'sleeping threads', when they wake up, they go to the 'runnable state' subjected to when the scheduler considers the thread oportuned to run.

Please excuse me if I am wrong, I recently woke up from sleep.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Yeah, this is no different from what I said, I just didn't explicitly mention the word 'runnable'
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Hi All,

Thanks for the replies,

Yeah the object which invokes wait() and counterparts should have a lock on that object;
otherwise it will throw illegalMonitorstateException.

Now i have a question. In the Java docs it is said that when we invoke wait the thread will be in the wait set of that object and when
notify is called on the same object it will notify the threads in the waitset of that object; Which then comes to runnable state and then
re acquires the lock and continues from where it stopped.

Q1.so does that mean when we call wait, sleep or any other API through which we block the thread; It well be moved to wait set which corresponds to
blocked state of thread or only in case of wait?

below is the code i have written.


The out put i get is always

writeData notifying
before write wait 3
0
2
readData notifying
Main Thread Ended
1
readData notifying
after writeData returning from wait0



Write thread looks ok to me . Can any body comment on read operation.

Q2.if we observe the read operation the list had [0,1,2] when reading it is printing 0 2 and then 1. Also note that both read threads t1 and t2 have
entered the synchronization block both are raising readData notify how can two thread enter the same synchronized block. I think there is some thing
i am missing?

Please correct me.

Thanks
Chandra
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Ikpefua Jacob-Obinyan wrote:
Stephan wrote:
after the specified amount of time is passed, and it will just continue to run

Hello Stephan...If havent forgotten what I studied about 'sleeping threads', when they wake up, they go to the 'runnable state' subjected to when the scheduler considers the thread oportuned to run.

Please excuse me if I am wrong, I recently woke up from sleep.


Stephan van Hulst wrote:Yeah, this is no different from what I said, I just didn't explicitly mention the word 'runnable'


I think that Jacob was specifically talking about, is that when a thread comes back from Thread.sleep(), then it will not start running immediately, but rather goes into the "runnable pool", waiting for its turn before it actually continues to run.

I'm sure this was what you meant too Stephan, just trying to be clear on the differences as they can easily be mixed up.

// Andreas
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Thanks Andreas.

Chandra, this wait set you refer to is indeed just the set of threads that are waiting to reacquire the lock on the object monitor. So they are not in waiting state, but in blocking state. Blocking mode is applicable to threads that have called the wait() methods, or are about to enter a synchronized block or method.
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
So does that mean wait set = Blocked state ?

we may call wait, sleep, yield ...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Yes, if a thread is in the wait set, that implies it's in the blocked state.

Is everything clear to you now?
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Hi,
I found the answers in the JLS and in this Thread

Actually wait, notify, notifyAll are all in the object Class; every object will have Wait set and lock. Wait set is used only by wait, notify and notifyAll methods only.

BlockedState is a generalized word used for wait set, sleeping and suspended threads. As suggested by Kathy siera its ok to use Blocked state/Blocked thread and no need to worry about the finer states with in blocked state. For finer states wait, notify, notifyAll will be in wait set (as in JLS) and sleep, yield, join not sure will be part of blocked state.

Thanks
Chandra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification in wait, notify and notifyAll methods