File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronized keyword usage Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "synchronized keyword usage" Watch "synchronized keyword usage" New topic
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
 
Threads others viewed
can a Class be synchronized?
volatile keyword
thread synch and inner classes
Problem with threads (concurrency)
The Difference Between StringBuffer & StringBuilder
developer file tools