• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Difference between synchronized method and synchrozed block

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can some body explain the difference between synchronized method and synchrozed block.

Thanks
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See it here
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patricia Samuel wrote:See it here



Its confusing me
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with a synchronized block you can choose what to synchronize on. With a synchronized method, an instance method always synchronizes on the instance, a static method always synchronizes on the class.



synchronized(this) can have any object you want to synchronize where as when a method is synchronized it will work with instance for which it is called.
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patricia Samuel wrote:with a synchronized block you can choose what to synchronize on. With a synchronized method, an instance method always synchronizes on the instance, a static method always synchronizes on the class.



synchronized(this) can have any object you want to synchronize where as when a method is synchronized it will work with instance for which it is called.



So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anwar Hussain wrote:So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???



It doesn't matter whether a whole method is synchronized or just a block. Synchronized always means the synchronized code section can be run by one thread at a time only.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anwar Hussain wrote:So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???




Is equivalent to :-


Form K&B
"When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object."
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic