File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Does each instance of the Object own a lock OR ..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Does each instance of the Object own a lock OR ....." Watch "Does each instance of the Object own a lock OR ....." New topic
Author

Does each instance of the Object own a lock OR .....

Kenny Smith
Greenhorn

Joined: Jan 08, 2002
Posts: 9
Does each instance of the Object own a lock OR
each synchronized method of the instance own a lock, so that the instance owns multiple locks?


Kenny Smith
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
First, the fact is that it's threads that are actually running and seeking & releasing locks. Every instance in the RAM is represented by at least a thread.
Then, let's take a close look at the lock issue:
When the instance (or a thread) is running and is up to the time to invoke a synchronized lock, it enters the stage of lock-seeking. If seeking fails, it may try again later. If seeking succeeds, the instance (or the thread) gets the lock and run the code inside the synchronized method and release the lock upon finishing. The instance (or the thread) can't do anything else simultaneously, except for that it starts another thread. The started thread may encounter synchronized method and goes through the same necessary stages.
Therefore, the question of "Whether an instance or the invoking method owns the synchronized method?" is answered by the fact that an instance (or a thread) can't do anything else when running in a invoking method of a synchronized method. Then, either of the two can be said to own the lock.
However, the question of "whether an instance can have multiple locks?" boils down to a question of "whether an instance can be run in multiple threads?". In most cases, that's not the case.


" Veni, vidi, vici "<br />" I came, I saw, I conquered "
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I'd just like to emphasise that any object instance has exactly one monitor lock. So if you have two synchronized methods a() and b(), only one of them can execute at any given time.
- Peter
Kenny Smith
Greenhorn

Joined: Jan 08, 2002
Posts: 9
Laudney,
You've said,
However, the question of "whether an instance can have multiple locks?" boils down to a question of "whether an instance can be run in multiple threads?". In most cases, that's not the case.
Refers to "most case".
So, what's the exception of "most case" where an instance can be run in multiple threads?
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
Well, in fact, I haven't been informed of any exceptions. I said "most cases" just for avoiding extremism.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Each Object instance has a single lock that must be obtained by any thread executing a synchronized method of that instance. Two threads may not grab the same lock.
That said, you could create multiple instances of Object as members of a parent Object. Instead of grabbing the lock (calling a synchronized method) on the parent Object, you could grab the lock of one of the member Objects. That way you could seem to have multiple locks on a single object.
...Mike B.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
"broadbear", sorry to be a bore but could you please review our naming policy and reregister? The reason behind it is that reading JavaRanch should be a bona fide thing to do in the workplace, and keeping a professional appearance is part of that.
- Peter
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
Sorry about that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does each instance of the Object own a lock OR .....