The moose likes Threads and Synchronization and the fly likes Synchroniztion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchroniztion" Watch "Synchroniztion" New topic


Naveen Kota

Joined: Sep 26, 2005
Posts: 2
What is the difference between a synchronized method and using a synchronized block. What difference does it make logically and which is more efficient

Naveen Kota<br />NMS Engineer
Tom Billings

Joined: Sep 12, 2005
Posts: 16
A synchronzed method always locks on this object (unless it's a static method in which case the lock is held on a class-level).

A synchronized block locks on any object, however, locking the this object with a synchronized block is similar to using a synchronized method. But, it may be more efficient to use a synchonized block if the lock isn't nesseccary throughout the whole method.

Hence, if you use the this object:

public void synchronized foo() {


is similar to:

public void foo() {
synchronized(this) {

I agree. Here's the link:
subject: Synchroniztion
It's not a secret anymore!