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.

[949 byte] By [Chiew_Boona] at [2007-11-27 0:39:15]
# 1

Without seeing how B is defined it is hard to answer the question. If the methods in B are not static synchronized methods then there is nothing to stop them from being called concurrently with your "transaction". To ensure atomicity you need to be able to assert control over all the code paths than can call those B methods.

davidholmesa at 2007-7-11 22:50:41 > top of Java-index,Core,Core APIs...
# 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.

Chiew_Boona at 2007-7-11 22:50:41 > top of Java-index,Core,Core APIs...
# 3
If the *only* code that calls the static methods of B is the instance method of the singleton class A, then making the instance method synchronized is sufficient.Of course you need to document this restriction/assumption about the use of B.
davidholmesa at 2007-7-11 22:50:41 > top of Java-index,Core,Core APIs...
# 4
Don't forget in order to guarantee atomicity you need to handle rolling back to a consistent state if something fails.
JasonChoya at 2007-7-11 22:50:42 > top of Java-index,Core,Core APIs...