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. Hello,

I am still getting the below

View Tutorial          By: pavithra at 2012-03-25 19:33:38

2. hi,
i need to connect to server named xxx,i

View Tutorial          By: annamalai at 2012-05-21 10:09:52

3. It's helpful to me.. to understand stand clear pic
View Tutorial          By: Ansari at 2010-08-18 10:22:54

4. thanks a lot
View Tutorial          By: Uzzal biswas at 2014-11-04 02:42:54

5. This post is about adding an application icon to a
View Tutorial          By: Rick at 2011-11-04 16:48:16

6. Does anybody know how to force using IPv6 by my la
View Tutorial          By: Marcin at 2011-05-17 05:24:51

7. great solution and brief explanation, love it.
View Tutorial          By: habesha at 2011-12-02 16:14:45

8. hello all,
i have this error.
Except

View Tutorial          By: daniel at 2012-08-12 18:50:04

9. In this code what should i add in place of "y
View Tutorial          By: manju at 2011-11-10 09:01:50

10. i don,t think so that it is sufficient to understa
View Tutorial          By: amit singh at 2009-05-27 06:54:20