Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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

objects and lock  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does each object in java have a corresponding lock?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jasmine alba:
Why does each object in java have a corresponding lock?



Sorry, I don't understand your question.

Please be more specific. Thanks.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every object in Java has a corresponding lock and that lock is used in multithreading enviornment to ensure the desirable access to the methods or the block of code.
When there are more than one thread working on the non-static methods (or the block of code in those methods.) than the lock taken on the object (the instance of some class) is taken by the thread and no other thread with the same instance of object can access those methods if they are synchronized. That means by synchronizing the method, you will ensure that any threads trying to run that method using the same instance will be prevented from simultaneous access.
But thread working with a different instance will not be affected, because it's acquiring a lock on the other instance.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!