Results 1 to 3 of 3
  1. #1
    Rituparna is offline Member
    Join Date
    Feb 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default Why wait(), notify(), notifyAll() declared in Object class?

    Hi friends,
    why is it that wait(), notify(), notifyAll() methods have been declared in the Object class and not in the Thread class? Is it possible to call these methods on the main thread?

    If we create a thread by a Runnable instance, then how could we access the methods of the Thread class?? can we import java.lang.Thread?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    It's because you can use any Object as a lock. These functions would actually be pretty useless if they were only in the Thread class.

    For example, say you have an Object and two methods that do different things with that Object. Many different Threads are accessing those two methods, but you want to synchronize those methods. How would you do that if wait(), notify(), etc were only in the Thread class?

    As for the rest of your questions, what happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    The static method Thread.currentThread() returns the current thread. There is no need to import anything from java.lang.

Similar Threads

  1. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 08:58 AM
  2. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  3. Problems using wait() and notifyAll() with locks
    By cuffJ in forum Threads and Synchronization
    Replies: 6
    Last Post: 08-18-2010, 03:21 AM
  4. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  5. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •