This may be a related question: Java assignment issues - is it atomic?
I have the same class as the OP that acts on a mutable string reference. But recruitment rarely happens. (basically this line is part of the server configuration, which only reboots when forced).
public class Test { private String s; public void setS(String str){ s = str; } public String getS(){ return s; } }
Several threads will beat this variable to read its value. What is the best way to make it “safe” without requiring performance degradation by declaring it unstable?
I'm currently heading towards ReadWriteLock, but as far as I understand, ReadWrite locks don't make it safe from thread caching? if some kind of synchronization doesn't happen? What does it mean that I returned the full circle back to use only the volatile
keyword?
Do I understand correctly? There is nothing that could “notify” other threads about a variable update in the main memory manually, so that they can update their local cache only once on the full moon?
volatile
on this seems redundant, given that the server application is designed to work for several months without a reboot. By then, that would have served several million readings. I think I could just set String as a static final and not let it mutate without a full application and restarting the JVM.
user986139
source share