Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Synchronised block in the main method  RSS feed

 
Greenhorn
Posts: 26
  • 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 ?



 
Master Rancher
Posts: 3275
33
  • 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: 26
  • 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
Master Rancher
Posts: 3275
33
  • 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: 26
  • 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
Master Rancher
Posts: 3275
33
  • 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: 26
  • 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
Master Rancher
Posts: 3275
33
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!