The easiest way to make V
thread safe is to make it immutable (by making final all its non static fields) then in A
you simply affect a new instance of V
in updateV
as next:
public class A {
private V v;
public synchronized V getV() {
return v;
}
public synchronized V updateV(A a, B b, Cc) {
//complex logic updating V
this.v = new V(...);
return v;
}
}
As you can see to make A
thread safe I simply added the keyword synchronized
to the getter and the setter to prevent concurrent read and write but if it is not needed in your case you can sill make v volatile
as next:
public class A {
private volatile V v;
public V getV() {
return v;
}
public V updateV(A a, B b, Cc) {
//complex logic updating V
this.v = new V(...);
return v;
}
}