Managing Multiple threads accessing a single instance of a class

Hi,

i have to redesign a class, say X, in such a way that i manage multiple threads accessing a single instance of the class, we cannot create multiple instances of X. The class looks like this:

Class X{

boolean isACalled=false;

boolean isInitCalled=false;

boolean isBCalled=false;

A(){

isACalled=true;

}

Init(){

if(!isACalled)

A();

B();

C();

isInitCalled=true;

}

B(){

if(!isACalled)

A();

isBCalled=true;

}

C(){

if(!isACalled)

A();

if(!isBCalled)

B();

}//end of class

Init is the method that would be invoked on the single instance of this class.

Now i cannot keep the flags as instance variables coz different threads would have differrent status of these flags at the same time, hence i can make them local, but if i make them local to one method, the others won't be able to check their status, so the only solution i can think of is to place all the flags in a hashtable local to method INIT AND INITIALIZE ALL OF them to false, as init would call other methods, it would pass the hashtable reference as an additional parameter, the methods would set the flags in the hashtable and it would be reflectecd in the original hashtable, and so all the methods can have access to the hashtable of flags and can perform their respective checks and setting of flags.

This all would be local to one thread, so there's no question of flags of one thread mixin with the flags of some other thread.

My question is :

Is this the best way, would this work?

In java, everything is pass by value, but if i pass the hashtable reference, would the changes made inside the called method to the hashtable key-value would be visible in the original hashtable declared inside the calling method of which the hashtable is local variable?

[1919 byte] By [kittykat13a] at [2007-11-27 8:41:50]
# 1
Variables can be made class-level by making it "static". Single copy of vars will be visible to all threads of the same class.However, the intent/requirement for this program is not clear. May be, you can re-phrase the question to improve clarity.
java_a at 2007-7-12 20:40:58 > top of Java-index,Core,Core APIs...
# 2
In Java object variables are passed "by copy of reference", and primitive variables "by value".The solution with HashMap/Hashtable you suggest is ok, but I think you should read about ThreadLocal class: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ThreadLocal.html
data-lossa at 2007-7-12 20:40:58 > top of Java-index,Core,Core APIs...
# 3
Why don't you call A() B () and C() in the constructor of the class and end all this agony?
ejpa at 2007-7-12 20:40:58 > top of Java-index,Core,Core APIs...