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



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. This works perfect for me. The only big problem is
View Tutorial          By: Eric at 2014-11-20 03:49:19

2. i didnot get the answer
View Tutorial          By: ramana at 2013-02-04 06:36:37

3. hiiiii
View Tutorial          By: hiiiiii at 2010-07-28 14:08:50

4. I tried to use excute this code to retreive record
View Tutorial          By: PL at 2013-08-27 02:58:55

5. its good and well performance
View Tutorial          By: siva.c at 2014-09-02 08:18:19

6. Thanks Thanks Thanks.....for you
But how

View Tutorial          By: jihad at 2009-02-15 04:48:53

7. can client use prinwriter?????
View Tutorial          By: sujay at 2009-02-20 00:23:33

8. sir i need some simple example of jdbc i cannot un
View Tutorial          By: Velkumar.s at 2013-05-15 07:08:37

9. This program little hard to understand ..please gi
View Tutorial          By: sarav at 2011-02-17 05:02:46

10. I am very comfortable with this example, its very
View Tutorial          By: Inam at 2010-11-07 03:04:46