• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wait , notify() ...explain this output  RSS feed

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I understood the concept of notify() and wait(), but confused with the output ... why the statement "Entry of gettttttttttt:1" get printed...

I have bolded the line in the output statement where there is a confusion



code
class Q
{
int n;
boolean valueSet=false;

synchronized void put(int i)
{
if(valueSet)
{

try
{
System.out.println(" Entry of Put :"+i);
wait();
System.out.println("\n Exit of put ");
}
catch(InterruptedException ieo)
{
System.out.println("\n Interrupt Caught ");
}
}

n=i;
valueSet=true;
System.out.println(" Put : "+n);
notify();
System.out.println(" What will be the next line");
}

synchronized void get()
{
if(!valueSet)
{
try
{
System.out.println("Entry of gettttttttttt:"+n);
wait();
System.out.println(" Get after wait ");
}
catch(InterruptedException ieo)
{
System.out.println("\n Interrupt Caught ");
}
}

System.out.println(" Got : "+n);
valueSet=false;
notify();
System.out.println(" Testing .......................");

}

}

class Producer implements Runnable
{
Q q;

Producer(Q obj)
{
q=obj;
new Thread(this,"Producer").start();
System.out.println("\n\nProducer hai ");
}

public void run()
{
int i=0;

{
while(true)
{
System.out.println(" In while ---- put");
q.put(i++);
System.out.println(" In while ---- put down");
}
}

}
}

class Consumer implements Runnable
{
Q q;

Consumer(Q obj)
{
System.out.println("\n Consumer hai ");
q=obj;

new Thread(this,"Consumer").start();

}

public void run()
{


while(true)
{
System.out.println(" In while ---- get" );
q.get();
System.out.println(" In while ---- get down");
}
}

}

class withnotify
{
public static void main(String[] args)
{
Q q=new Q();

new Producer(q);
new Consumer(q);


for(int i=0;i<1000;i++);


System.exit(1);
}
}




Output

Producer hai

Consumer hai
In while ---- put
Put : 0
What will be the next line
In while ---- put down
In while ---- put
Entry of Put :1
In while ---- get
Got : 0
Testing .......................

Exit of put
Put : 1
In while ---- get down
In while ---- get
What will be the next line
Got : 1
Testing .......................
In while ---- get down
In while ---- get
[b]Notify() should pass the control to void put() which is on wait(), but instead the below statement get executed.... notify() gets avoided[b]
Entry of gettttttttttt:1
In while ---- put down
In while ---- put
Put : 2
What will be the next line
Get after wait
Got : 2
Testing .......................
In while ---- get down
In while ---- get
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivit: Notify() should pass the control to void put() which is on wait(), but instead the below statement get executed.... notify() gets avoided


That is how it is happening that is why you are getting an alternate PUT and a GET output. If you see in the output a PUT: <number> is always followed by a GET: <same number>

However, the print statements after notify are not something that you must expect as ordered. Actually, after you call notify, the Producer and the consumer Threads will run parallely. So, the order in which you get the output is totally based on how the threads are scheduled.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, that but why the the bolden thing is happening....
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shivit Agarwal:
I know, that but why the the bolden thing is happening....


Your bolden thing is not happening.
How do you determine that the control after notify is not going to put?
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:


Your bolden thing is not happening.
How do you determine that the control after notify is not going to put?


Please read the Q. twice before posting the reply. That is the most lame answer i encountered in this forum. Anybody can say looking at the program Producer->consumer->producer->consumer ....likewise the output will produced.

Have you even read the output w.r.t question, for the bolden text. I got the answer from the different forum. Please don't just reply to the query unless you read/understand the problem.
[ May 12, 2008: Message edited by: Shivit Agarwal ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivit:
Please read the Q. twice before posting the reply. That is the most lame answer i encountered in this forum.

You have guts man. Firstly you have posted a question in such a sorry state (bad english, lot of waste system outs which anyways can not help determine the execution of the program (although i dont blame you because it needs technical knowledge to assert that they are a waste), no code tags(75 posts and been told a few times about putting the code tags.), saying that you have "bolded" the line that you have not(I thought you know how to edit your post if you have missed it for the first time.)).
On top of all this I took the pain to answer your question at best i could understand (It was terribly tough to go through such a horrendous code which is not formatted and full of ugly system outs) and now you have guts to talk like this.

Shivit:
Anybody can say looking at the program Producer->consumer->producer->consumer ....likewise the output will produced.


Really you understood that? Great but you did not get what i said in this post:

you are getting an alternate PUT and a GET output. If you see in the output a PUT: <number> is always followed by a GET: <same number>


To help you, what you said and what i have written is the same.

Shivit:
Have you even read the output w.r.t question, for the bolden text

Oh really, can you show me the text in bold?

Shivit:I got the answer from the different forum.

Great then you must have had the courtesy of posting a link to your other post or mention the answer here for the benefit of other ranchers.

Shivit:Please don't just reply to the query unless you read/understand the problem.

I know, but it does not apply to topics which are hopelessly posted. One can just try his/her best to understand and reply.

I would not get into a discussion as to what I asked you was lame or not because i dont think its worth talking with you on those aspects.

(Sorry fellow ranchers, i tried to stick to the "be nice" policy the best i could.)

[ May 13, 2008: Message edited by: Nitesh Kant ]
[ May 13, 2008: Message edited by: Nitesh Kant ]
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitesh,

I am with you. We all know how well you answer here. Keep up the good work and help us out.

Thanks,
Ugender
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ugender for the kind words.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify() does not release the lock at that moment when it gets called. It releases lock after completing the synchronized block in which it is executing.
But the other threads waiting on the synch object have been notified, so they are now in runnable state.
After the consumer has exited from the synch block, now there are 2 threads eligible for a run and its all upto the scheduler to pick up one of these based on the scheduling policy.
This time, again consumer thread got picked up and hence the result in bold.
The result might not be reproduced exactly.

Please point out if I am wrong.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Amitabh Mehra
Thanks a lot. I got it.

@Nitesh
would have really appreciated if you would have spend the same time in giving the solution rather than writing such wonderful lines. May be I was bit harsh ,if do sorry but the answer was very short.
I am quite passionate about coding, so i use to play with the code. Even here i was just trying 2 figure out how the code output is being processed , so you saw hell lot of System.out. statement.
Thanks anwyay...

No issues....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!