jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes Could Class be synchronized ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Could Class be synchronized ?" Watch "Could Class be synchronized ?" New topic

Could Class be synchronized ?

Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
Can class be synchronized ,if yes?
Then what is the need and why?

if ne body knew then please do favour by conveying.

Abhijeet Nalawade

Joined: Apr 01, 2007
Posts: 20
No..class cannot be Synchronized. We can have synchronized blocks or methods.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
Your query is prone to ambiguous interpretation . What exactly do you mean by 'class synchronization'?

If you mean synchronizing on the class object of some class,then yes.

As for the need,well it could be for preventing race conditions arising out of multiple threads concurrently trying to access the static variables of a class.
Rohith Kumar Singh

Joined: Aug 27, 2008
Posts: 6
By saying 'if a class can be synchronized...' if you meant if a class member i.e., a static member can be synchronized then the answer is Yes. You just need to acquire lock on the Class object of the associated Java class.
[ August 27, 2008: Message edited by: Bear Bibeault ]
Kevin Smither
Ranch Hand

Joined: Oct 22, 2007
Posts: 30
The question "can class be synchronized" does not make sense. A block of code can synchronize on the class.. for example: synchronize(MyClass.class) so that the code associated with all instances of that class cannot be re-entrant in that block. You can also synchronize on a single instance such as synchronize(this) or synchronize(xyz) where xyz is an instance. Make sense?
I agree. Here's the link: http://aspose.com/file-tools
subject: Could Class be synchronized ?
It's not a secret anymore!