• 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

synchronized keyword usage

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain the difference between synchronized(this) and synchronized(MyClass.class) usage.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Devadoss Poornachari:
Can anyone explain the difference between synchronized(this) and synchronized(MyClass.class) usage.

The first synchronizes on an object, i.e. a single instance of the class; as you no doubt know, there can be any number of instances of a class. The second synchronizes on the class object itself; for a given class, there is just one class object. As a consequence, the last style of synchronization allows only one thread into the block at a time, whereas the first style allows one thread per instance.
Incidentally,Is equivalent toAs you know, static methods are associated with a class rather than any specific instance of a class, so it shouldn't surprise you thatIs equivalent toDoes that help?
- Peter
reply
    Bookmark Topic Watch Topic
  • New Topic