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



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. nice example . and good job.
View Tutorial          By: mohit sinha at 2011-04-07 03:19:01

2. Comment: Just for future readers: (apart from the
View Tutorial          By: Anonymous at 2013-05-17 08:53:20

3. thanks,Gud job
View Tutorial          By: junaid sheikh at 2012-11-21 16:24:27

4. Thank you very much you resolved my issue.
View Tutorial          By: ketan at 2012-02-01 11:16:47

5. Protip: you could add some inline assembly with th
View Tutorial          By: Robin Degen at 2011-08-16 23:47:04

6. PROGRAM TO FIND OUT SMALLER NU:-

cl

View Tutorial          By: ishaan gogna at 2012-05-09 19:00:49

7. i have to write a calculator program for very ve
View Tutorial          By: maryam at 2012-12-31 10:21:55

8. really helpul........
pls mail me some more

View Tutorial          By: shivani at 2010-07-17 01:47:30

9. thats good. but not for the begineers. because in
View Tutorial          By: jaywant topno at 2009-10-31 10:05:43

10. nice one
View Tutorial          By: dhusor at 2012-05-08 17:38:50