File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread Watch "thread New topic
Author

thread's

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi
please come up with something, this question has created a Black hole in my mind.
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
Hi Nitin,
Change all IF to WHILE loop. Then every thing will work
fine.
Pratap
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
The above code is working fine but i just want's to understand that code.please somebody explain it.
 
wood burning stoves
 
subject: thread's
 
Similar Threads
producer/consumer problem
Thread's
Thread's
Thread's
Explain me this output (notify)