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. I discover unlimited calendar.this calendar use of
View Tutorial          By: Lalit Kumbhani at 2011-12-31 16:02:06

2. Very good explanation along with the example. Can
View Tutorial          By: Divya at 2010-09-21 03:05:10

3. hi....
can u please show me the code for fi

View Tutorial          By: pooja at 2012-07-08 16:08:08

4. Awesome Explanation...:-)
View Tutorial          By: satz at 2011-06-28 01:45:56

5. Can you explain when the user input through the ab
View Tutorial          By: mujmil at 2008-02-11 18:46:00

6. syntax of java program in looping statement
View Tutorial          By: lokesh at 2009-03-25 06:57:56

7. teacher namin na si Engr. Lumasag nakakasira ng ba
View Tutorial          By: rossy at 2011-10-06 06:12:53

8. i got an error msg at runtime of jdbc program with
View Tutorial          By: madhu sudan at 2012-06-17 02:52:53

9. hai, i tried the abvoe code, m getting the follwin
View Tutorial          By: anith at 2011-03-13 06:02:31

10. I have to ask; Where do we put the text boxes?
View Tutorial          By: Jeremy at 2010-10-07 14:37:07