The moose likes Threads and Synchronization and the fly likes object lock Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "object lock" Watch "object lock" New topic
Author

object lock

Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
I was wondering about the following example:

Is the lock on object released?
Thanks,
Mike
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
The lock, in this case, is on the object refered to by the object variable. Once it enters the synchronized block it gets the lock associated with that object and holds it until it leaves the block. As soon as it leaves the block the lock is released.
hope that helped


Dave
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Maybe it's worth amplifying Dave's point: the lock is released as soon as the thread leaves the block, regardless of how it leaves it. Whether it leaves it because the thread moves beyond the last curly brace, or because of a return (as in your code), a break, continue, or an exception, the lock is released.
- Peter
 
 
subject: object lock
 
Threads others viewed
wait() in synchronized code ??
Is a monitor the same thing as a lock?
how can i achieve a 100% thread safty method ?
Creating multiple children threads
About Object lock
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com