File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized Blocks " Watch "Synchronized Blocks " New topic

Synchronized Blocks

vendikonda sravan
Ranch Hand

Joined: May 09, 2010
Posts: 38
Can Some buddy help me to know the difference between a synchronized Method /synchronized block ? what different ways to declare a synchronized block ? and why do we call it a lock on object were we declare a method as Synchronized?

Thanks in advance
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

I am sure googling it will give you a lot of answers.

In short synchronizing blocks will try to obtain the lock of an object that you specify before it executes code. You can reduce the span of synchronized code this way and attempt to improve efficiency. A synchronized method will lock on the instance of the current object or the Class object that the current object belongs to. This depends on whether the method is static / non-static.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Good question, and a cause of much confusion. There is really only one form of
synchronization - the synchronized block of code. All you need to remember is
that before you operate on an object that another thread can also change, you
must secure its lock using synchronized. Marking a method as "synchronized" is
handy (less typing) but also misleading. We talk about synchronized methods
but it is really the current object that is locked, not the code.

Jim ... ...

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