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 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchroniztion" Watch "Synchroniztion" New topic
Author

Synchroniztion

Naveen Kota
Greenhorn

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Synchroniztion
 
Similar Threads
Threads
Synchronized block and methods
synchronized
synchronized
about threads, using locks of the objects in synchronized blocks