Kamis, 03 April 2014

Why wait, notify and notifyAll is defined in Object Class and not on Thread class in Java




Why wait, notify and notifyAll is declared in Object Class instead of Thread is famous core java interview question which is asked during all levels of Java interview ranging from 2 years, 4years to quite senior level position on java development. Beauty of this question is that it reflect what does interviewee knows about wait notify mechanism, how does it sees whole wait and notify feature and whether his understanding is not shallow on this topic. Like Why Multiple inheritance is not supported in Java or why String is final in java there could be multiple answers of why wait and notify is defined in Object class and every one could justify there reason.  






Why Wait notify method is declared in Object Class and not in Thread in JavaIn my all interview experience I found that wait and notify still remains most confusing for most of Java programmer specially up-to 2 to 3 years and if they asked to write code using wait and notify they often struggle. So if you are going for any Java interview make sure you have sound knowledge of wait and notify mechanism as well as you are comfortable writing code using wait and notify like Produce Consumer problem or implementing Blocking queue etc. by the way This article is in continuation of  my earlier article related to wait and notify e.g. Why Wait and notify requires to be called from Synchronized block or method and  Difference between wait, sleep and yield method in Java , if you haven’t read you may found interesting.








Reason Why Wait , Notify and NotifyAll are in Object Class.





Here are some thoughts on why they should not be in Thread class which make sense to me :





1) Wait and notify is not just normal methods or synchronization utility, more than that they are communication mechanism between two threads in Java. And Object class is correct place to make them available for every object if this mechanism is not available via any java keyword like synchronized. Remember synchronized and wait notify are two different area and don’t confuse that they are same or related. Synchronized is to provide mutual exclusion and ensuring thread safety of Java class like race condition while wait and notify are communication mechanism between two thread.





2 )Locks are made available on per Object basis, which is another reason wait and notify is declared in Object class rather then Thread class.





3) In Java in order to enter critical section of code, Threads needs lock and they wait for lock, they don't know which threads holds lock instead they just know the lock is hold by some thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock. this analogy fits with wait and notify being on object class rather than thread in Java.





These are just my thoughts on why wait and notify method is declared in Object class rather than Thread in Java and you have different version than me. In reality its another design decision made by Java designer like not supporting Operator overloading in Java. Anyway please post if you have any other convincing reason why wait and notify method should be in Object class and not on Thread.




Update:

@Lipido has made an insightful comment , which is worth adding here. read his comment for full text




"Java is based on Hoare's monitors idea (http://en.wikipedia.org/wiki/Monitor_%28synchronization%29). In Java all object has a monitor. Threads waits on monitors so, to perform a wait, we need 2 parameters:

- a Thread

- a monitor (any object)



In the Java design, the thread can not be specified, it is always the current thread running the code. However, we can specify the monitor (which is the object we call wait on). This is a good design, because if we could make any other thread to wait on a desired monitor, this would lead to an "intrusion", posing difficulties on designing/programming concurrent programs. Remember that in Java all operations that are intrusive in another thread's execution are deprecated (e.g. stop())."






Some Java articles on Thread you may like






























Source:http://javarevisited.blogspot.com/2012/02/why-wait-notify-and-notifyall-is.html

Tidak ada komentar:

Posting Komentar