I have a multithreaded application where I need to ensure all threads are referring to the latest version of a long. I was thinking of using AtomicLong for this purpose. The AtomicLong will be in one class and other classes will need to get the value and also set the value.
private final AtomicLong key;
public ClassHoldingLong(){
this.key = new AtomicLong(System.currenttimemillis());
}
public long getKey() {
return key.get();
}
public void setKey(long key) {
this.key.set(key);
}
Is this fine to do or should the getter be AtomicLong itself and someone calls .get() on the AtomicLong rather than this class holding the variable calling it for another class. From what I read, these getters setters don't need to be synchronized but not sure if that is only if AtomicLong is the return type.
This class does have additional information it holds such as statistics and additional functionality. I just included this snippet of code for an example