Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between synchronized method and synchrozed block

 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See it here
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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???
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • 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.
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic