Why Wait, Notify Together With Notifyall Is Defined Inwards Object Degree Together With Non On Thread Degree Inwards Java

Why to wait, notify together with notifyAll is declared inwards Object Class instead of Thread is famous centre coffee interview query which is asked during all levels of Java interview ranging from 2 years, 4years to quite senior marker seat on coffee development. Beauty of this query is that it reverberate what does interviewee knows almost expect notify mechanism, how does it sees whole expect and notify characteristic together with whether his agreement is non shallow on this topic. Like Why Multiple inheritance is non supported inwards Java or why String is end inwards java in that location could live on multiple answers of why expect together with notify is defined inwards Object class together with every ane could justify in that location reason.  



Reason Why Wait , Notify together with NotifyAll are inwards Object Class.

Here are roughly thoughts on why they should non live on inwards Thread shape which make feel to me :

1) Wait together with notify is non simply normal methods or synchronization utility, more than that they are communication machinery betwixt 2 threads inwards Java. And Object shape is correct place to brand them available for every object if this machinery is non available via any coffee keyword similar synchronized. Remember synchronized together with expect notify are 2 unlike expanse together with don’t confuse that they are same or related. Synchronized is to furnish usual exclusion together with ensuring thread security of Java class similar race status spell expect together with notify are communication machinery betwixt 2 thread.


2 )Locks are made available on per Object basis, which is roughly other argue expect together with notify is declared inwards Object shape rather together with therefore Thread class.

3) In Java inwards guild to piece of occupation inwards critical department of code, Threads needs lock together with they expect for lock, they don't know which threads holds lock instead they simply know the lock is concord by some thread together with they should expect for lock instead of knowing which thread is within the synchronized block and cry for them to unloose lock. this analogy fits amongst expect together with notify existence on object shape rather than thread inwards Java.

These are simply my thoughts on why expect together with notify method is declared inwards Object shape rather than Thread inwards Java together with you have unlike version than me. In reality its roughly other blueprint conclusion made yesteryear Java designer similar not supporting Operator overloading inwards Java. Anyway delight post if y'all bring whatever other convincing argue why expect together with notify method should live on in Object shape together with non on Thread.

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

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

In the Java design, the thread tin non live on specified, it is ever the electrical current thread running the code. However, nosotros tin specify the monitor (which is the object nosotros telephone phone expect on). This is a skillful design, because if nosotros could brand whatever other thread to expect on a desired monitor, this would Pb to an "intrusion", posing difficulties on designing/programming concurrent programs. Remember that inwards Java all operations that are intrusive inwards roughly other thread's execution are deprecated (e.g. stop())."

Further Learning
Multithreading together with Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
When to role Thread or Runnable interface inwards Java?

Komentar

Postingan populer dari blog ini

Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons

Top V Websites For Practicing Information Structures Together With Algorithms For Coding Interviews Free