File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Working of notify()/wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Working of notify()/wait()" Watch "Working of notify()/wait()" New topic
Author

Working of notify()/wait()

babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Hi,
I don't know whether this is the right forum to ask this question or. Apology in advance if the question is lightweight one :-).

Q.

Assumption1. There is a class A, having two methods namely m1 & m2. Both m1 and m2 have the wait(); and notify(); coded.

Assumption2. There are two thread T1 and T2. T1 access the m1 method of A, where as T2 will access the m2 method.

Now at the time of execution T1 encounters wait(); of m1 [Note that
notify();].

At this point of time what will happen to T2? Can T2 access m2 at this particular phase?[note T2 is yet to encounter wait(); , or is it required for T2 to wait until T1 encounters the notify(); ?


Be bold.
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Please note, one point I missed,which is required to describe the above scenario completely.
T1 has encountered wait(); of m1, but not yet the notify(); of m1().

Thanks in advance.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Are both threads are accessing the same instance of class A? Otherwise the two threads do not interact in any way, and there would be little to ask about. Or perhaps m1() and m2() are static methods, in which class instances are irrelevant. I will assume what I think is the most common case, m1() and m2() are non-static, and T1 and T2 are accessing the same instance of A.

Use of wait() and notify() implies the methods are synchronized, or contain synchronized blocks which sync on the same instance that is used to call wait() and notify(). So once T1 starts running m1(), T2 cannot start m2(), because it doesn't have the lock. Until T1 executes wait(), because the wait() method gives up the lock while waiting. Then T2 can run m2(), which eventually calls notify(), which makes T1. T1 cannot run immediately because T2 still has the lock (it had to have the lock in order to call notify(). But once T2 leaves the sync block in m2() (or leaves m2() entirely), T1 can reacquire the lock, and resume execution starting immediately after the wait() statement.

In the event this explanation did not make sense, it might help to post more code showing what you're doing. I've made several assumptions along the way, and if they're wrong, this just becomes more confusing.


"I'm not back." - Bill Harding, Twister
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
Post the src code. that will help grasp what you are asking better..
chandra garre
Greenhorn

Joined: Nov 08, 2005
Posts: 7
There is an interesting twist
We are sure that these two methods are synchoronized and hence only one thread will be in.

1. Since T2 now is in a synchronized method and notifyAll/notify is called, this tells that it has indicated all the other threads that there is no need to wait, they can continue, as of now "they" is T1.
2. notifyAll is not blocking, it only indicates that its fine to proceed, but since T2 has the ownership of the lock for the object, T1 , which has a signal to move ahead fails to do so, since it is still waiting for the lock now owned by T2.
3. Only after T2 complets the method which has notifyAll, will T1 run to get the lock and proceed.


Interesting twist isnt it ... thats the fun of wait and notifyall :-)
chandra garre
Greenhorn

Joined: Nov 08, 2005
Posts: 7
Also one point to remember, if m1() and m2() both have wait and then notify all like this

m1()
{
blah
blah
wait();
blah
notifyAll();
}


m2()
{
blah
blah
wait();
blah
notifyAll();
}


Here as such both T1 and T2 would be blocked indefintely till someone fires a notifyall from outside.

Am i missing something !
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Hi Chandra,
You have got the exact scenario. Now your answer has raised some more light weight query in my mind.
The code you have given looks like
m1()
{
1.1blah
1.2blah
wait();
1.3blah
1.4blah
notify();
}

m2()
{
2.1blah
2.2blah
wait();
2.3blah
2.4blah
notify();
}

Say in the main thread the code is
Thread1.m1();
Thread2.m2();

What will happen in this case?
What will the order of execution? Will both start together? But m1() is called before m2()?
Waiting for your anser eagerly.
Babai.
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Hi,
Can anyone please help to solve the issue mentioned above?.
Bhaumik.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working of notify()/wait()