This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt regarding synchronized blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding synchronized blocks" Watch "Doubt regarding synchronized blocks" New topic
Author

Doubt regarding synchronized blocks

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

I am studying synchronization and thread interaction. Please have a look at the following class:



In the main() thread , we are instantiating the Calculator class. Next we are passing that calculator reference to create 3 distinct objects of Reader class and calling start() method on these objects. My question is that how come is it possible that all 3 different Reader objects are locking on the same calculator instance. Obviously when we pass a reference variable , we pass copy of bits that refer to the same Calculator object on the heap right. How come that 3 Reader threads are locking on same calculator object? The rule says that if one thread has acquired a lock on an object, no other thread can acquire that lock unless that thread releases the lock. Correct?


~ Mansukh
Florin Vasile
Greenhorn

Joined: Feb 14, 2013
Posts: 8
I believe that the wait method releases the locks. so each thread will acquire the lock on the calculator object and release it on wait. After you execute notifyall each one will obtain the lock (each at a time) and continue to completion.


FVasi
OCPJP 6, OCEJPAD 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Florin Vasile, welcome to the Ranch
Try the Java Tutorials.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Florin Vasile wrote:I believe that the wait method releases the locks. so each thread will acquire the lock on the calculator object and release it on wait. After you execute notifyall each one will obtain the lock (each at a time) and continue to completion.


I need to ask you another thing Florin. How do I know that the calculation has not completed before the 3 Reader threads call wait and release locks on the calculator instance. What stops the JVM from running the calculator.start() thread first? Then all the three threads could be left waiting for something that has actually already been completed. Right?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:Florin Vasile, welcome to the Ranch
Try the Java Tutorials.


There are a lot of topics in the Java link you provided. Could you be more specific as to which one of them should I study to seek an answer to my question Ritchie?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding synchronized blocks
 
Similar Threads
Question on Thread example in Kathy Sierra 1.4 book
blocking and waiting
Question regarding notify() and wait()
Threading discrepancies
blocking and waiting