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



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. Is it possible to define a class such that the pri
View Tutorial          By: Smithy at 2012-05-01 22:50:58

2. Article is good but really lacks somewhat clarity
View Tutorial          By: Ashwin Perti at 2009-09-14 04:02:39

3. tis s very useful and easy to capture all persons
View Tutorial          By: Divya at 2013-12-19 05:42:32

4. awesome...its very understandable one..thnq..:)
View Tutorial          By: sharan at 2010-02-08 10:58:17

5. Very good explanation specially overlapped memory
View Tutorial          By: MANISH RASTOGI at 2010-06-22 05:44:07

6. 510
1020
2040
4080
816

View Tutorial          By: sky at 2010-07-17 06:54:46

7. very nice session
View Tutorial          By: Rajendra singh rathod at 2012-03-16 13:32:01

8. Many thanks for your nice explanation.
View Tutorial          By: Zahra at 2012-04-10 06:03:56

9. is it possible to change the increment variable?
View Tutorial          By: Lundi at 2013-04-08 21:21:13

10. it's very good to understand...and it is helpful f
View Tutorial          By: Rakesh at 2009-12-26 00:14:59