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



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 have a computer science lab final on tuesday (09
View Tutorial          By: kudas at 2009-06-07 10:55:06

2. notify does NOT wake up the first thread waiting.
View Tutorial          By: Armin at 2009-07-26 07:25:09

3. nice one
View Tutorial          By: sarvesh at 2015-03-12 05:16:10

4. great example for beginners. Thank you
View Tutorial          By: kumar at 2011-11-24 08:15:14

5. I think it is the best article for freshers.thanks
View Tutorial          By: srikanth at 2010-11-01 23:27:53

6. I need to know the parameters that we need to pass
View Tutorial          By: puru at 2008-09-08 00:33:46

7. a pretty good site for learners
View Tutorial          By: chaitra at 2011-12-15 10:09:14

8. Can you give some examples of do while & pleas
View Tutorial          By: mhilan at 2011-09-01 04:54:13

9. Very nice introduction to j2me well you should als
View Tutorial          By: Bikashjit at 2012-08-29 08:53:28

10. this is really very useful because of this site i
View Tutorial          By: meenakshi shukla at 2012-09-15 05:09:49