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



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. Very nice article.. thanks
View Tutorial          By: Basu at 2010-05-29 05:52:00

2. very good cod
View Tutorial          By: moly at 2007-03-13 08:17:40

3. Thank you for the tutorial. I want to create a cat
View Tutorial          By: Lue at 2012-07-09 10:40:08

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

5. Good
View Tutorial          By: Siva Sreekanth at 2013-10-01 16:29:55

6. You have mentioned that
to use this program

View Tutorial          By: Naasik at 2012-08-05 00:15:57

7. I am getting error in setContentView(R.layout.main
View Tutorial          By: Malick at 2013-06-30 06:57:30

8. your code helps a lot.....
thanx......

View Tutorial          By: beh at 2009-08-19 06:06:20

9. its useful code.
but i want to write in a f

View Tutorial          By: SARWAR at 2011-08-02 06:26:37

10. Can you please tell me about parameter and validat
View Tutorial          By: Atul at 2010-09-08 04:48:00