File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "objects and lock" Watch "objects and lock" New topic

objects and lock

jasmine alba

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

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

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:
subject: objects and lock
It's not a secret anymore!