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. Sir please can you tell me that how can i find occ
View Tutorial          By: Saad at 2013-01-20 11:14:39

2. I think this code is just simple enough to be foll
View Tutorial          By: WAQ at 2010-11-24 10:41:58

3. lots of thanks for this given example........hope
View Tutorial          By: virgi crisostomo at 2010-08-18 17:18:08

4. hi friends im the biginer of java
how to co

View Tutorial          By: Rock at 2013-05-14 09:27:19

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

6. nested for loop
View Tutorial          By: SAJAN at 2011-03-15 12:42:36

7. site is very good explanation is very clear throug
View Tutorial          By: sridevi at 2013-03-20 15:26:29

8. 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

9. i am run code this rectify this error please some
View Tutorial          By: indiran at 2010-01-28 01:45:56

10. Hello, I have executed the above code and it works
View Tutorial          By: Sana at 2010-02-01 03:49:44