Difference bewteen notify() and notifyAll()

Discussion in 'Java' started by mallikk, Oct 24, 2007.

  1. mallikk

    mallikk

    Joined:
    Oct 22, 2007
    Messages:
    2
    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

    ----------------------------
    mallikk, Oct 24, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    13
    Views:
    3,916
    Filip Larsen
    Jul 13, 2004
  2. vj
    Replies:
    17
    Views:
    23,646
    jessie143143
    Oct 21, 2012
  3. Vera
    Replies:
    5
    Views:
    827
    blmblm@myrealbox.com
    Oct 13, 2006
  4. Bryan
    Replies:
    12
    Views:
    685
    Thomas Hawtin
    Dec 19, 2006
  5. Danny
    Replies:
    18
    Views:
    803
    Daniel Pitts
    Dec 15, 2008
Loading...

Share This Page