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



Comment Added by : princess

Comment Added at : 2011-11-30 12:11:57

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
hope 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. it is working....great
View Tutorial          By: mahesh maduranga at 2011-03-27 23:01:57

2. i have been trained in java stream. But i dont kno
View Tutorial          By: savio at 2013-01-23 10:15:51

3. hi sir i want to know source code of this program;
View Tutorial          By: rishabh at 2010-08-21 09:30:31

4. Thanks for a nice article !!!
View Tutorial          By: Piyush Sharma at 2011-12-15 12:24:06

5. Hi
Gr8 work......!!!!
thanks...!!!

View Tutorial          By: Antim Jain at 2010-07-12 23:44:14

6. This is really helpful, i'm planning to also add a
View Tutorial          By: JhunZhen at 2011-07-09 21:42:32

7. Can anybody tell the steps to go ahead with the ex
View Tutorial          By: Sunpenn at 2009-10-12 03:57:58

8. can any plz tell me how to create a calculator in
View Tutorial          By: sam at 2010-11-13 06:53:04

9. can anybody suggest me to write a java prog. to ke
View Tutorial          By: Giri at 2015-03-23 17:56:58

10. Hi,
I want to run a bat file, below are the

View Tutorial          By: Rajshekar at 2011-10-28 06:07:06