• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Synchroniztion

 
Greenhorn
Posts: 2
  • 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 a synchronized method and using a synchronized block. What difference does it make logically and which is more efficient
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A synchronzed method always locks on this object (unless it's a static method in which case the lock is held on a class-level).

A synchronized block locks on any object, however, locking the this object with a synchronized block is similar to using a synchronized method. But, it may be more efficient to use a synchonized block if the lock isn't nesseccary throughout the whole method.

Hence, if you use the this object:

public void synchronized foo() {

}

is similar to:

public void foo() {
synchronized(this) {

}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic