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. It is a good tutorial but I think the "Instru
View Tutorial          By: Jaypee at 2010-10-10 22:40:50

2. Excellent Example...... To understand the concept
View Tutorial          By: SUNNY at 2009-02-24 23:41:04

3. Write a times table programme. The programme shoul
View Tutorial          By: Hannah Jambalos at 2014-09-03 12:07:34

4. the default construrctor does not give the defalut
View Tutorial          By: muhammad abdullah at 2011-04-11 13:16:20

5. Thank you
View Tutorial          By: dara at 2009-12-19 02:46:52

6. Each right shift divides number by 2...
View Tutorial          By: kunal at 2013-06-18 03:43:54

7. Very nice tutorial.It helped me a lot cos i am a b
View Tutorial          By: Fara at 2011-07-01 10:53:44

8. How can i read PDF files from external memory(SD c
View Tutorial          By: Aswanth at 2014-03-31 09:46:26

9. Even i got d same error. can u plz tell me where t
View Tutorial          By: Priya at 2011-03-14 12:16:54

10. It work fine with message below 160 chars. How can
View Tutorial          By: Milind at 2011-12-15 06:20:33