5 Replies Latest reply: Jan 7, 2007 9:23 AM by JosAH RSS

    why notify() and notifyall() is in Object class

    807607
      pls help me

      why notify() and notifyall() methods which are related to thread are define in Object class,instead of defining in Thread class
        • 1. Re: why notify() and notifyall() is in Object class
          800322
          Because the thread wouldn't know which object holds the monitor. The object in return knows which threads are waiting on its monitor, so it could tell them.
          • 2. Re: why notify() and notifyall() is in Object class
            807607
            wait, notify, and notifyall are used by threads, and actually shouldn't be called on thread objects ever at all (one of my pet peeves with Java)
            • 3. Re: why notify() and notifyall() is in Object class
              JosAH
              wait, notify, and notifyall are used by threads, and actually
              shouldn't be called on thread objects ever at all (one of my pet
              peeves with Java)
              Why not? It would make a nice entry in the IOJJJ (International Obfuscated
              Java Juggling Jamboree) if it existed ;-)

              kind regards,

              Jos
              • 4. Re: why notify() and notifyall() is in Object class
                807607
                shouldn't be called on thread objects ever
                at all (one of my pet
                peeves with Java)
                Why not? It would make a nice entry in the IOJJJ
                (International Obfuscated
                Java Juggling Jamboree) if it existed ;-)
                First of all, sorry for my bad english. It's early here :)

                Second of all, the way that Thread is implemented, at least on windows, it that calling Thread.join does a wait() on that thread object. Thus, if you call notify() on a thread, you could actually be waking up joined threads, which wouldn't be good. And if you called wait, you could get woken up if the thread finishes which violates the contract of wait and notify to some extent and also relies on an undocumented feature.

                Overall, while some consider this a bug in java, I see it as an implementation choice. I just wish it were documented and that wait, notify, and notifyall were deprecated in Thread.
                • 5. Re: why notify() and notifyall() is in Object class
                  JosAH
                  shouldn't be called on thread objects ever
                  at all (one of my pet peeves with Java)
                  Why not? It would make a nice entry in the IOJJJ (International
                  Obfuscated Java Juggling Jamboree) if it existed ;-)
                  First of all, sorry for my bad english. It's early here :)
                  Nah, it's just a late a rainy Sunday afternoon here, so no problem ;-)
                  Second of all, the way that Thread is implemented, at least on
                  windows, it that calling Thread.join does a wait() on that thread object.
                  Thus, if you call notify() on a thread, you could actually be waking up
                  joined threads, which wouldn't be good. And if you called wait, you
                  could get woken up if the thread finishes which violates the contract of
                  wait and notify to some extent and also relies on an undocumented
                  feature.
                  I take back my previous remark: it could not just be a nice little entry in
                  the IOJJJ, it would be a great entry in that contest! ;-)

                  kind regards,

                  Jos

                  ps. your example is the first example that clearly gives a reason for
                  spurious wakeups I've ever read; thanks; joined threads, I've got to
                  remember that example. <scribble, scribble, scribble/> done. ;-)