Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized keyword usage  RSS feed

 
Devadoss Poornachari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the difference between synchronized(this) and synchronized(MyClass.class) usage.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!