This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
 
Similar Threads
Thread states, notify, notifyAll, wait
is struts thread safe
What is a mutex?
locking using notify/wait
Question about exception thrown from synchronized block of lock method