• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between synchronized method and block

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Can anyone tell me whats the difference between synchronized method and block, expecting some good logical answers.
 
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi karan can you please clearly specify your query by explaining which block are you asking for???
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The syntax says it all. For a method it must synch on the whole class.
For a block it only synchs on the referenced object, the class containing the block is not locked.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several key differences:

1) synchronized code always use objects as locks to prevent other threads from entering the synchronized block. For instance methods, they are synchronized on the 'this' reference, and for static methods they are synchronized on the instance of the Class object method belongs to. In blocks if code using the synchronized(object) {} syntax you can use any object as the lock

2) Because of 1) you have a lot more granularity with the synchronized(object) {} syntax rather than synchronized method. You can have different methods synchronize on different objects, or different parts of a method synchronized on different objects.

3) Best practice is to minimize the code inside synchronized blocks because they prevent concurrent threads from executing - losing any advantage of multi-threaded applications. Synchronized blocks help let those portions of a method that do not access shared resources to be run simultaneously while still keeping those parts that need to be shared thread-safe.

To combine 2 and 3, lets setup this scenario:

You have a Customer which has a Shopping Cart and some Recommendations. Your client is allowed to have multiple threads operating on the Customer at once. You have three methods for accessing data to the customer:

In this scenario, you couldn't add an item to the cart in one thread while adding recommendations in another thread, even though they don't access the same shared data because you are synchronizing on the Customer object. But if you changed it to:

Now you can change recommendations while modifying/displaying the cart and vice-versa and so run more code in parallel.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between

[ September 24, 2008: Message edited by: Ramesh Kumar Swarnkar ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Kumar Swarnkar:
what is the difference between



The first one will compile. The second one won't.

Henry
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic