• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread's

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
please come up with something, this question has created a Black hole in my mind.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
Change all IF to WHILE loop. Then every thing will work
fine.
Pratap
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code is working fine but i just want's to understand that code.please somebody explain it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic