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. any help please I'm getting this exception

View Tutorial          By: Bull at 2012-01-02 17:22:18

2. i want struts flow
View Tutorial          By: suresh at 2013-03-28 10:28:53

3. hi,
i have tried to download the sun api fr

View Tutorial          By: raghu at 2009-01-07 05:45:33

4. It would he helpful to know difference between a l
View Tutorial          By: Tosh at 2012-04-28 18:00:20

5. Hi, I have one problem in spinner items. I want to
View Tutorial          By: soodi at 2011-10-18 18:15:58

6. i want to download a file after the URL connection
View Tutorial          By: mohit at 2011-11-09 11:16:07

7. what items do i add?
View Tutorial          By: Camo at 2015-09-30 23:02:10

8. hi,
i am safeer, i am try this program in

View Tutorial          By: Safi at 2009-05-22 22:32:04

9. pls help on me to program in c++, write a program
View Tutorial          By: Hamid at 2014-12-18 20:41:40

10. Thank you for this tutorial. Thank you all the mor
View Tutorial          By: sd at 2013-09-09 05:38:52