aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized block vs synchronized method" Watch "Synchronized block vs synchronized method" New topic
Author

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


BEE MBA PMP SCJP-6
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
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
Greenhorn

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: 396

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: http://aspose.com/file-tools
 
subject: Synchronized block vs synchronized method
 
Similar Threads
exception in sync block
static synchronized block
Question for java guru
synchronized block vs. synchronized method
servlet and thread synchronization