Forums Register Login

Thread Interaction (Wait-NotifyAll)

+Pie Number of slices to send: Send
Hello Friends,
I am having following queries related to the code enclosed :
* In synchronized block of Calculator..Why we have to give "this" as a parameter..Why is it wrong if we give "c" , After all , we invoke wait on lock "c" , so as rules have to notify on lock "c" only.
* Why do we need to pass "Calculator" object in "App" objects ?
Thanks

(1 cow)
+Pie Number of slices to send: Send
 

In synchronized block of Calculator..Why we have to give "this" as a parameter..Why is it wrong if we give "c" , After all , we invoke wait on lock "c" , so as rules have to notify on lock "c" only.


Because "this" in Calculator and "c" in App are the same at that point (both refer to object #1). "c" in App points to a Calculator instance, but within that Calculator instance to lock that instance you need to synchronize on the whole Calculator. Note that the Calculator c within Calculator serves no purpose and the code would still compile if it were removed. Locking/synchronizing on that c would not only fail (because locking a null object throws a null pointer exception), but if that calculator were initialized, it would be locking on a different Calculator than the one in App.

Why do we need to pass "Calculator" object in "App" objects ?


Because the Calculator defined in main is in a static method. Static methods do not require an instance of the class to execute, they are "shared" between all instances. If you didn't want to pass the Calculator to the App, you could make the Calculator at the App class level static, allowing the main method to access it. (but making it shared between all threads) Main can't access the Calculator right now because there could be many different Calculators around, one for each App instance and Java doesn't know which Calculator instance would be correct to access.
+Pie Number of slices to send: Send
 

Tina Smith wrote:

Why do we need to pass "Calculator" object in "App" objects ?



Thank you Tina , I have understood the "this" concept but still couldn't get the clarity in the second question. I will appreciate if you could extend it by giving some example.
Thanks Again!!!

 
+Pie Number of slices to send: Send
So when you enter into main(), there are no objects created (well nothing you created anyway). You are just executing a method, main() and that method belongs to the App class. You create an object, an App (let's say app1), and then you create a second App (app2). If you tried to access the Calculator from main, which App's calculator would you be accessing?

Or here's another question, what if you had no Apps. What would the Calculator field mean then? The calculator belongs to an instance of an App. The main method requires a minimum of zero App instances to be called. So the calculator doesn't make sense to be available to the main method, it wouldn't know what to do with it.

If might be clearer that the Calculator belongs to the App if you pull the main method out into a different class. Then you can see clearly what belongs where...


Now the main method is totally separate from the actual App object. It's just using the features of the object and just looking at the AppDriver class it's not clear that the App even has a Calculator field.

If the Calculator didn't belong to the App - if no matter how many Apps you had, there was still one calculator, it would make sense to access a Calculator even if there were no apps. And that is what the static keyword is for. It marks an object (method or variable) as belonging to the App class, and that it is shared between all instances. Then main could see it because neither of them require an App exist in order to work.
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 984 times.
Similar Threads
notify() vs notifyAll() in this code
Threads starve! Shouldn't yield() or wait()/notify() help?
Threads - wait() and notifyAll()
Synchronization example in KS&BB
wait() and notifyAll()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:33:44.