aspose file tools
The moose likes Threads and Synchronization and the fly likes Why wait(), notify(), notifyAll() declared in Object class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Why wait(), notify(), notifyAll() declared in Object class?" Watch "Why wait(), notify(), notifyAll() declared in Object class?" New topic
Author

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

Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

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?

With regards,
rituparna
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Rituparna Duttagupta wrote: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?

Please SearchFirst. I this question has been asked quite a few times before.

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?

Thread.currentThread() returns a reference to the (surprise surprise) current Thread.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Rituparna: These behaviors correctly belong to Object because
it is objects that must be protected from unwanted changes. Java
allows you to lock any object, even:

Object lockObject = new Object();

Jim ...


BEE MBA PMP SCJP-6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Jim Hoglund wrote:Rituparna: These behaviors correctly belong to Object because it is objects that must be protected from unwanted changes. Java allows you to lock any object


Here is an excerpt from another topic -- my response to whether the Object class or the Thread class is the correct place for these methods.

Henry Wong wrote:
As already mentioned, the reason these methods are part of the Object class, is because any object can be used for synchronization, and hence, any object should be able to be used for notification. Having these methods as part of the Object class is, of course, better than the Thread class, in this regard.

However, there is a flaw. The flaw being that there isn't necessary a one to one mapping between a mutex and its condition variable. And the Java (synchronization, wait, notify) design enforces this. Prior to Java 5, this mapping is removed by classes designed just for being a lock and condition variables. At Java 5, the Lock and Condition class were added to deal with this.

So... IMHO, the answer should be that neither answer is correct. The best design, in retrospect, should have been something done entirely using classes.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

thank you all, i got my answer now.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why wait(), notify(), notifyAll() declared in Object class?
 
Similar Threads
Interview q
Threads
wait, notify and notifyAll
Regarding Thread class
Object Class