• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronised block in the main method  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code about the synchronisation between threads, according to the output generated why is the control being transferred to the execution of the new thread despite the lock being acquired for the same object "dt" in the main method ?



 
Rancher
Posts: 2350
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why is the control being transferred to the execution of the new thread despite the lock being acquired for the same object "dt" in the main method ?


Read the API doc about what calling the wait() method does regarding monitor locks.
 
saurabhmehta mehta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

why is the control being transferred to the execution of the new thread despite the lock being acquired for the same object "dt" in the main method ?


Read the API doc about what calling the wait() method does regarding monitor locks.



Wait() method when called on an object, releases the lock on the object and is resumed once notify() or notifyAll() is called on the same object. According to this logic shouldn't it be notified when the if condition is true and notify() is called?
 
Norm Radder
Rancher
Posts: 2350
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the thread will be made ready to access the lock the next chance the OS has to give it the lock.  If others have the lock, it will have to wait until the lock is available.
Read the API doc for the notify method.
 
saurabhmehta mehta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Yes, the thread will be made ready to access the lock the next chance the OS has to give it the lock.  If others have the lock, it will have to wait until the lock is available.
Read the API doc for the notify method.



The notify method according to the code was called 3 times. So all these 3 times the main thread was not woken up and given the lock? And the main thread is only woken up when the loop is complete. Doesn't this seem a little against the logic for the notify() method? I have changed the while condition to ++i <=10 still the same case. why are all the notify() calls being ignored then?
 
Norm Radder
Rancher
Posts: 2350
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

all these 3 times the main thread was not woken up and given the lock?


main won't get the lock UNTIL the lock is released by the lock's current owner.

See the API doc for the notify() method.
 
saurabhmehta mehta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

all these 3 times the main thread was not woken up and given the lock?


main won't get the lock UNTIL the lock is released by the lock's current owner.

See the API doc for the notify() method.



Doesn't the lock gets released once the sleep() duration is over ?
 
Norm Radder
Rancher
Posts: 2350
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again - read the API doc for the sleep() method and see what it says about monitors
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!