| Author |
synchronized keyword usage
|
Devadoss Poornachari
Greenhorn
Joined: Apr 15, 2003
Posts: 2
|
|
|
Can anyone explain the difference between synchronized(this) and synchronized(MyClass.class) usage.
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
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
|
 |
 |
|
|
subject: synchronized keyword usage
|
|
|