Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Difference bewteen notify() and notifyAll()

Reply
Thread Tools

Difference bewteen notify() and notifyAll()

 
 
mallikk mallikk is offline
Junior Member
Join Date: Oct 2007
Posts: 2
 
      10-24-2007
I am new to the java world. I couldn't get a satisifying answer for difference between notify() , notifyAll().
To my understanding notify() should send notification to only one of the waiting threads and notifyAll() to all those waiting threads.
In my below example , i am using ONLY notify() but still other threads are gettign notified.

Please do help me in understanding why notify() is sending notification to all waiting threads.

Many thanks in advance.

CODE:

------
// reader class

class Reader extends Thread
{
Caliculator calc;

public Reader (Caliculator c)
{
calc = c;
}

public void run()
{
synchronized(calc)
{
try
{
System.out.println(Thread.currentThread().getName( ) + " is waiting ....");
calc.wait();
System.out.println(Thread.currentThread().getName( ) + " got notified ..");
System.out.println(Thread.currentThread().getName( ) + " - total : " + calc.sum);

}
catch(InterruptedException ie) {}
}

}

public static void main(String [] args)
{
Caliculator cal = new Caliculator();
Reader r1 = new Reader(cal);
r1.setName("Reader-1");
Reader r2 = new Reader(cal);
r2.setName("Reader-2");
Reader r3 = new Reader(cal);
r3.setName("Reader-3");

cal.start();
r1.start();
r2.start();
r3.start();



System.out.println("Program ends here");
}
}

//caliculator class

class Caliculator extends Thread
{
int sum=0;
public void run()
{
synchronized(this)
{
for (int index=0;index < 100 ; index++)
{
sum +=index;
}
try
{
Thread.sleep(2000);
}
catch(InterruptedException ex)
{
}

notify();
}
}
}

------------------------------------------------------------------------

output:


Reader-1 is waiting ....
Reader-2 is waiting ....
Reader-3 is waiting ....
Reader-2 got notified ..
Reader-2 - total : 4950
Reader-1 got notified ..
Reader-1 - total : 4950
Reader-3 got notified ..
Reader-3 - total : 4950

----------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
naive question: what is the primary difference bewteen w7 home pro and ultimate? MSDN newsgroup Windows 64bit 11 07-12-2009 02:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
BGP bewteen 25 routers Jeff Cisco 1 01-15-2005 02:03 PM
EnableSessionState=readonly and difference between InProc and StateServer mode Jeffry van de Vuurst ASP .Net 0 05-18-2004 10:18 AM
2 Pics Scanned @ width 1024 pixels, and DPI 72 and 300. What Is the Photographic Quality Difference? Ubiquitous Digital Photography 8 12-24-2003 05:02 PM



Advertisments