Ensuring Atomicity via Synchronizing
Hi,
I have a singleton class A which will initialize objects in the first load and cache them.
I have another class B with 3 static methods. These 3 methods has to be run as a transaction.
So in a particular method in my Class A, I have the following:
Class A method:
void syncnronized methodA(){
for ( ........){
synchronized ( B.class){
B.method1();
B.method2();
B.method3();
}
}
}
My question is will this ensure the atomicity of 3 methods call? If not, any opinion on how can I ensure that all methods in Class B is run before another thread tries to access class B.
Thank you very much.
# 2
Hi,
Basically, only the a method in the singleton class A will access Class B.
Class B looks like this:
class B {
private static variableA;
private static variableB;
static method1() {
//do something and set variables A and B
}
static method2() {
//process variableA
}
static method3() {
//process variableB
}
}
Class A {
private static A singletonInstanceA;
static A getInstance() {
//method to get instance of A
}
synchronized void someMethod() {
//not sure if this will ensure the atomicity of the 3 methods
synchronized(B.class) {
B.method1();
B.method2();
B.method3();
}
}
}
There are not synchronization done on the Class B. I've only applied they synchronized key word to all methods in Class A ( as it's a singleton ).
Please advise.
Thank you.