Comment on Tutorial - wait(), notify() and notifyAll() in Java - A tutorial By Jagan



Comment Added by : princess

Comment Added at : 2011-11-30 12:09:38

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
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



Ask a Question

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. Hii... pls how can I create a aerver with an inter
View Tutorial          By: WAZIRI at 2010-03-16 11:12:11

2. hi i am new to JSP wht is class.forname in the cod
View Tutorial          By: chanti at 2011-05-07 03:02:26

3. Good examples. However... I am testing for a Hex v
View Tutorial          By: Doug Whitehead at 2008-11-29 17:10:43

4. Good intro..
View Tutorial          By: thirunavukkarasu at 2015-01-30 17:32:10

5. What exactly is d directry for d folder Media?? It
View Tutorial          By: Oraclematrix at 2012-01-15 21:40:54

6. nice to learn abt it
View Tutorial          By: aice at 2012-08-25 12:49:35

7. Thanks a lot, a long time I search for solution fo
View Tutorial          By: Jonhnes at 2013-02-16 17:17:47

8. Hello,

When I put the source code i

View Tutorial          By: Pascal at 2013-03-25 09:12:47

9. I am using VS 2010 Express and MS Excel 2007 where
View Tutorial          By: Alphin at 2012-07-06 10:57:07

10. good work.
if there is a ValidatorForm the

View Tutorial          By: floriano at 2008-10-09 07:23:16