Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could Class be synchronized ?

 
Sachindra Pratap
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Can class be synchronized ,if yes?
Then what is the need and why?

if ne body knew then please do favour by conveying.

regards
sachindra
 
Abhijeet Nalawade
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No..class cannot be Synchronized. We can have synchronized blocks or methods.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers,
Rohit
[ August 27, 2008: Message edited by: Bear Bibeault ]
 
Kevin Smither
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic