aspose file tools*
The moose likes Beginning Java and the fly likes objects and lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "objects and lock" Watch "objects and lock" New topic
Author

objects and lock

jasmine alba
Greenhorn

Joined: Mar 26, 2008
Posts: 4
Why does each object in java have a corresponding lock?
Chang Tzu-yu
Greenhorn

Joined: Sep 17, 2007
Posts: 16
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.


Chang Tzu-yu<br /> <br />SCJP 1.4
Ravinder Partap
Greenhorn

Joined: Feb 03, 2008
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: objects and lock