Why Wait, Notify Too Notifyall Is Defined Inwards Object Bird Too Non On Thread Bird Inwards Java
Why to wait, notify as well as notifyAll is declared inwards Object Class instead of Thread is famous centre coffee interview enquiry which is asked during all levels of Java interview ranging from 2 years, 4years to quite senior flat seat on coffee development. Beauty of this enquiry is that it reverberate what does interviewee knows almost await notify mechanism, how does it sees whole await and notify characteristic as well as whether his agreement is non shallow on this topic. Like Why Multiple inheritance is non supported inwards Java or why String is in conclusion inwards java at that spot could travel multiple answers of why await as well as notify is defined inwards Object class as well as every i could justify at that spot reason.
Why Wait as well as notify requires to travel called from Synchronized block or method and Difference betwixt wait, slumber as well as yield method inwards Java , if you lot haven’t read you lot may flora interesting.
Reason Why Wait , Notify as well as NotifyAll are inwards Object Class.
Here are around thoughts on why they should non travel inwards Thread flat which make feel to me :
1) Wait as well as notify is non only normal methods or synchronization utility, more than that they are communication machinery betwixt ii threads inwards Java. And Object flat is correct place to brand them available for every object if this machinery is non available via any coffee keyword similar synchronized. Remember synchronized as well as await notify are ii unlike expanse as well as don’t confuse that they are same or related. Synchronized is to supply usual exclusion as well as ensuring thread security of Java class similar race status spell await as well as notify are communication machinery betwixt ii thread.
2 )Locks are made available on per Object basis, which is around other argue await as well as notify is declared inwards Object flat rather hence Thread class.
3) In Java inwards gild to instruct inwards critical department of code, Threads needs lock as well as they await for lock, they don't know which threads holds lock instead they only know the lock is agree by some thread as well as they should await for lock instead of knowing which thread is within the synchronized block and squall for them to loose lock. this analogy fits amongst await as well as notify beingness on object flat rather than thread inwards Java.
These are only my thoughts on why await as well as notify method is declared inwards Object flat rather than Thread inwards Java as well as you have unlike version than me. In reality its around other blueprint determination made yesteryear Java designer similar not supporting Operator overloading inwards Java. Anyway delight postal service if you lot convey whatsoever other convincing argue why await as well as notify method should travel in Object flat as well as non on Thread.
Update:
@Lipido has made an insightful comment , which is worth adding here. read his comment for amount 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 dismiss non travel specified, it is ever the electrical flow thread running the code. However, nosotros tin dismiss specify the monitor (which is the object nosotros telephone band await on). This is a expert design, because if nosotros could brand whatsoever other thread to await 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 around other thread's execution are deprecated (e.g. stop())."
Further Learning
Multithreading as well as Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
When to purpose Thread or Runnable interface inwards Java?
0 Response to "Why Wait, Notify Too Notifyall Is Defined Inwards Object Bird Too Non On Thread Bird Inwards Java"
Post a Comment