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



Ask a Question

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. You have to run the compiled PortScanner with two
View Tutorial          By: KRook at 2009-10-15 04:11:53

2. which method is called if i call overriden method
View Tutorial          By: sarah at 2012-02-01 13:36:10

3. After coming to second page again I want to go to
View Tutorial          By: Bhagi at 2012-09-23 06:25:54

4. hiii frds i want to run this program can i run usi
View Tutorial          By: arun kumar ojha at 2011-07-20 00:59:10

5. nice example for understanding abstract class,,,bu
View Tutorial          By: Aavez Sk at 2012-01-17 09:44:36

6. the default implementation of validate returns nul
View Tutorial          By: Jerry at 2009-02-01 11:12:06

7. at
OK
at+cmff=1
OK
at+

View Tutorial          By: shrinivas at 2009-03-07 23:16:51

8. Unfortunatelly this a BAD example of to communicat
View Tutorial          By: Mladen Covic at 2013-03-28 15:08:04

9. Declaring int result within the method will change
View Tutorial          By: Soph at 2013-02-06 06:11:39

10. Very well Explained Thank you Hog .
View Tutorial          By: Asad at 2010-08-10 23:59:29