Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working of notify()/wait()

 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(); ?
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the src code. that will help grasp what you are asking better..
 
chandra garre
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone please help to solve the issue mentioned above?.
Bhaumik.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic