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. thanks i am fresher to j2me i understand the conce
View Tutorial          By: anusha at 2010-04-20 00:32:10

2. Windows XP
libmySQL.dll in package MySQL5.1

View Tutorial          By: Bingo at 2009-11-07 20:11:52

3. u doing good
View Tutorial          By: matsiko at 2009-04-14 08:05:00

4. #include<iostream>
#include<conio.

View Tutorial          By: Zohaib Farooq at 2011-05-23 07:58:44

5. hi

im trying to run this project an

View Tutorial          By: VoyRufino at 2010-01-26 10:03:11

6. Its working perfectly for me.Thanks so much for pr
View Tutorial          By: Sunpenn at 2009-10-13 00:53:09

7. be very careful. use of put (or any method inheri
View Tutorial          By: d at 2009-10-14 09:55:23

8. i have to retrieve data from database which is on
View Tutorial          By: abhishek at 2012-04-05 05:04:01

9. i added respective code to the Manifest in every c
View Tutorial          By: niko at 2013-04-19 18:23:41

10. simply super...every one can understand
View Tutorial          By: prashanth at 2013-03-10 12:20:25