aspose file tools*
The moose likes Beginning Java and the fly likes locking an object other than this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "locking an object other than this" Watch "locking an object other than this" New topic
Author

locking an object other than this

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
when do you use this kind of synchronization

synchronized(mutex)
{
//
}

mutex represents the object whose object level lock should be acquired before entering the lock.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
When you synchronize a whole method like this...



...you know that no other thread will ever get to execute this code if the lock is already acquired by one thread.

Hence, if you want to decrease the scope of the lock, letting other threads to execute part of method, you can better control the scope of the lock by means of using synchronized blocks. Like this...



This way you may gain some performance by means of reducing the scope of the lock.

Also, the new concurrent package in Java Tiger (JSE 1.5) will offer you even more control over the locking system. You might like to give it a look at java.util.concurrent API.

Regards,
Edwin Dalorzo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: locking an object other than this