File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Synchronized block vs synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized block vs synchronized method" Watch "Synchronized block vs synchronized method" New topic

Synchronized block vs synchronized method

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
What is the difference between them? Synchronized block do a better performance than methods. Please throw some light on this.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

both are required to have an object lock. but by using synchronized block, you can *minimal the critical section[synchronization scope]*
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
At run time they are exactly the same. Labeling a method as synchronized is just
a shorthand way to label the code block when 'this' is the lock object. There is no
performance difference. Jim ... ...

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Back around JDK 1.2 or so, there were some performance differences between the two - even though I agree, they should be exactly the same. Nonetheless they were compiled to different bytecodes, and optimized differently in the JVM. Don't know why. I haven't cared enough to test since then, but I would guess that any significant performance differences may well have been erased since then. Or maybe not.

Either way, I don't recommend worrying about such performance differences unless and until you have an observable performance problem, and can verify that it makes a difference. Other considerations are usually more important.
Sujeet Kumar Jaiswal

Joined: Mar 07, 2010
Posts: 17
One major difference is that:

With synchronized methods, the thread will have the lock of 'this' object only. But by using synchronized block we can pass any object as the parameter and lock of that particular object will be given to thread.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sujeet Kumar Jaiswal wrote:With synchronized methods, the thread will have the lock of 'this' object only.

if the method is static , then it holds the class level lock.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

Last time I looked the method level one set the ACC_SYNCHRONIZED flag and the other (block one) added the relevant monitor byte code, soo just on the basis the first has less code it should be more efficient if probably unmeasurable ;-) the only thing that did occur is how well the various Java 5-6 lock optimisations would code with the block version or not as it would seem to be easier to optimise with the method level version maybe (can't really back that up ;-) )

As everyone else has pointed out block level ones can be more efficient but the method levels ones are easier to read and simpler to use, seen soo much newbie code were they've shot themselves in the foot with happens before ordering etc by trying to get clever with block scope when the difference between that and method scope would have been negligible.

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
I agree. Here's the link:
subject: Synchronized block vs synchronized method
jQuery in Action, 3rd edition