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. nice tutorial with discussion
View Tutorial          By: Md Firoz at 2012-01-07 11:00:29

2. Assalamu alaikum saaliya sir,

Could

View Tutorial          By: Jaffer sulaiman at 2014-08-23 06:01:20

3. It works , i had a lot of trouble but finally , i
View Tutorial          By: Omer at 2009-06-30 06:05:17

4. quite helpful
View Tutorial          By: kezyRoan at 2011-06-11 03:58:52

5. Here is my issue. The sql which comes thru the JTD
View Tutorial          By: Kumar at 2012-02-21 02:42:08

6. public void playSound(String filename) {

View Tutorial          By: Rowan at 2014-07-17 10:26:02

7. I want to send a message using mobile browser to s
View Tutorial          By: Richa Bhatia at 2009-12-08 22:33:36

8. This is very good article.
View Tutorial          By: Vipin Joshi at 2008-03-27 13:53:26

9. thankx a lot...i've been trying so hard to learn t
View Tutorial          By: bhuwan at 2009-04-08 23:30:45

10. very nice i now understand what the allocated part
View Tutorial          By: Digistr - digistrscape (youtube.com/digistrscape) at 2010-01-30 01:17:19