How can I make class level synchronization?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nihar_Patil
Posted On:   Wednesday, January 18, 2006 10:04 AM

I have a class C having methods m1 & m2. Also I have


threads t1 & t2 accessing methods m1 & m2 respectively. If


I created 2 different objects of class C then will the


execution on 2 objects be synchronized?

Re: How can I make class level synchronization?

Posted By:   Anonymous  
Posted On:   Wednesday, January 18, 2006 11:57 AM

No, there will be two instances of class C having their own set of variables that are processed in their m1 & m2 methods. The puropose of synchronization is to ensure the integrity of the variables that are processed by the synchronized method.


If your scenario involves processing that has to be synchronized in all the instances of class C (e.g. c1.m1 must not do this processing while c2.m1 executes), then I would suggest that you wrap this processing in a separate class, B, that has only one instance, and pass that instance to the C class. In the B class you then code the must-be-synchronized-processing in a synchronized method (or two...)
About | Sitemap | Contact