Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Synchronized Blocks  RSS feed

 
Ranch Hand
Posts: 38
  • 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
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 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.
 
Ranch Hand
Posts: 525
  • 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 ... ...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!