This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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 ... ...
 
Can't .... do .... plaid .... So I did this tiny ad instead:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!