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. hi
Can you anyone please post me the entir

View Tutorial          By: yasmeen at 2009-05-25 02:42:44

2. Good work!

Few changes: polish oper

View Tutorial          By: Karol Z. at 2010-06-02 09:09:52

3. Hi All..
Wen i'm running the code..the foll

View Tutorial          By: mounika at 2008-04-26 14:27:30

4. Question, how much is Struts 2 used today? As a fr
View Tutorial          By: Marco at 2012-10-04 06:34:43

5. Why not a simple array list of threads?
wha

View Tutorial          By: Ranadheer Machineni at 2011-08-18 12:30:35

6. this code is not working properly buddies. please
View Tutorial          By: hammad at 2011-06-10 01:53:39

7. Can u please tell me how to compile and execute a
View Tutorial          By: RJ at 2010-04-20 01:30:10

8. Thanks for the description. Am loading my actionse
View Tutorial          By: sridevi at 2012-01-13 10:15:40

9. This is really helpful.
View Tutorial          By: srikanth at 2009-03-11 08:04:04

10. HI your explanation is absolutely correct but I am
View Tutorial          By: Anil R. Chinchawade at 2010-07-28 02:02:01