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



Related Tutorials

Java program to get location meta data from an image

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

Archived Comments

1. Hi Ashish, You just have to create an object of SM
View Tutorial          By: Ramlak at 2007-03-13 09:22:51

2. answers to the questions asked by ranjan in commen
View Tutorial          By: A Sadist at 2012-07-05 12:13:45

3. It's very clear explanation. thank you
View Tutorial          By: miroot at 2012-12-27 17:46:14

4. how to return the boolean value. what is the data
View Tutorial          By: karthick at 2013-02-07 10:21:28

5. simple and exelent programs
View Tutorial          By: prasad at 2012-08-29 00:05:04

6. why the code had error in status???
View Tutorial          By: matuz at 2011-11-13 11:35:34

7. nice example
View Tutorial          By: pavang at 2010-07-20 01:16:22

8. i am newly in java. please send basic sample progr
View Tutorial          By: bhuvana at 2012-11-21 06:50:01

9. My code:

$hex=array('C4028000','457

View Tutorial          By: Mark at 2015-02-02 14:42:56

10. it's very usefull to the users who were in startin
View Tutorial          By: Harichandana at 2013-02-28 04:37:17