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



Comment Added by : princess

Comment Added at : 2011-11-30 12:09:38

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
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. This is a great article that deals with the evolut
View Tutorial          By: JPS SASTRY at 2009-02-11 21:29:56

2. Best
Thanks

View Tutorial          By: Sujit at 2009-01-12 22:38:08

3. 1
View Tutorial          By: hamza at 2015-06-08 15:09:29

4. But I have the same error ("javax.net.ssl.SSL
View Tutorial          By: azeemuddin at 2010-03-04 07:25:33

5. Hi,
Very Nice explanation ,I tried both mem

View Tutorial          By: Harsh Verma at 2013-01-30 13:43:05

6. I HAVE ONE DOUBT.

public class Anim

View Tutorial          By: georgy at 2009-10-22 02:46:10

7. Hi,

Just wonder if there is a way t

View Tutorial          By: Bank at 2007-12-18 04:30:26

8. Hi, I need the complete working code for sending a
View Tutorial          By: prakash at 2010-05-24 09:14:59

9. its amazing for me i learn a lot from it thanks..
View Tutorial          By: sufyan at 2014-10-26 14:39:34

10. I tried ....so this is best solution for understa
View Tutorial          By: rehnuma at 2015-03-30 12:29:38