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 locking an object other than this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "locking an object other than this" Watch "locking an object other than this" New topic

locking an object other than this

sai donthneni
Ranch Hand

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


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... 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.

Edwin Dalorzo
I agree. Here's the link:
subject: locking an object other than this
jQuery in Action, 3rd edition