Forums Register Login

thread's

+Pie Number of slices to send: Send
class Q
{
int n;
boolean valueset=false;
synchronized int get()
{
if(!valueset)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("interruptedexception caught");
}
System.out.println("GOT:"+n);
valueset=false;
notify();
return n;
}
synchronized void put(int n)
{
if(valueset)
try
{
System.out.println("put method");
wait();
}
catch(InterruptedException e)
{
System.out.println("interruptedException caught");
}
this.n=n;
valueset=true;
System.out.println("put:"+n);
notify();
}
}
class producer implements Runnable
{
Q q;
producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class fixed
{
public static void main(String[]args)
{
Q q=new Q();
new producer(q);
new consumer(q);
System.out.println("pressc ontrol c to terminate");
}
}
sir,
i just want's to know how the above programme is working.I have an idea about it,my idea is
producer thread will start and it will produce a value then this thread will go in the wait mode and it will wake the other thread through notify method, then same thing will be done by got() method.
For eg.
Let's compare the two method's,put() and got()
1)put() method will produce a value which in turn will be consumed by the got() method of the second thread.
2)then after consuming, the got method() will invoke the put() method which will again produce a new value and this procedure will keep on going because of infinite loop's in the run method.
My question is when does the if condition get's tested in the case of got().
my guess is when the notify of got()wake the other thread before that it just test's the if condition of it's own method.Am i right, please let me know if am wrong and correct me.
In the case of put()first of all an if condition would be tested if it's true it will go in a waiting state otherwise it will bypass the try and catch block and then assign the value to the global variable n which is accessible in both the method's after that the (setvalue varaible)will become true then notify will be invoked but before it invoke's the got()method, it will test it's own condition which is true as a result it will enter the wait mode, then other thread will come into picture.If am right please tell me or expalin the way this programme work's.
complete reference page 300
+Pie Number of slices to send: Send
hi
please come up with something, this question has created a Black hole in my mind.
+Pie Number of slices to send: Send
Hi Nitin,
Change all IF to WHILE loop. Then every thing will work
fine.
Pratap
+Pie Number of slices to send: Send
The above code is working fine but i just want's to understand that code.please somebody explain it.
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 813 times.
Similar Threads
Explain me this output (notify)
Thread's
producer/consumer problem
Thread's
Thread's
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:14:11.