File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized Blocks

 
vendikonda sravan
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic