Comment on Tutorial - wait(), notify() and notifyAll() in Java - A tutorial By Jagan



Comment Added by : princess

Comment Added at : 2011-11-30 12:06:22

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
look at this..
may help u...

class product
{
int contents=0;
boolean available = false;
synchronized int get()
{
if(available==false)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptException caught");
}

System.out.println("consume:"+contents);
System.out.println("hello");
available=false;
notifyAll();
return contents;
}
synchronized void put(int num)
{
if(available==true)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
contents=num;
System.out.println("produce:"+contents);
System.out.println("Welcome");
notifyAll();
}
}
class producer extends Thread
{
product p;
producer(product p)
{
this.p=p;
this.start();
}
public void run()
{
int i=0;
p.put(++i);
}
}
class consumer extends Thread
{
product p;
consumer(product p)
{
this.p=p;
this.start();
}
public void run()
{
p.get();
}
}
public class interthreadcon
{
public static void main(String[] args)
{
product s = new product();
new producer(s);
new consumer(s);
}
}


View Tutorial



Ask a Question

Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. thankx a lot...i've been trying so hard to learn t
View Tutorial          By: bhuwan at 2009-04-08 23:30:45

2. thanks bro it helped me to answer my facu
View Tutorial          By: kasmin at 2010-01-08 08:11:32

3. Hello,

I got an error like (Wed Ma

View Tutorial          By: narayanan at 2010-03-17 05:56:21

4. top notch
View Tutorial          By: anu at 2010-03-15 01:46:38

5. Can anybody send the full working code for sending
View Tutorial          By: Atul at 2008-11-26 04:47:51

6. Is it possible to define a class such that the pri
View Tutorial          By: Smithy at 2012-05-01 22:50:58

7. Thx, this helped a lot.

How can I I

View Tutorial          By: Marko at 2008-10-09 01:32:37

8. great.....
View Tutorial          By: rashid at 2009-12-07 03:22:46

9. Thanks a ton! :)
View Tutorial          By: Prateek at 2015-03-12 11:40:08

10. wow amzing
View Tutorial          By: Anonymous at 2009-04-23 01:44:43