Commit 7934fdcc authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

Updated comments for DynamicConstant

parent 99914daa
Pipeline #35600943 passed with stage
in 1 minute and 51 seconds
......@@ -26,6 +26,9 @@ import java.util.function.Supplier;
* This class is thread-safe.
*/
public class DynamicConstant<T> {
// Using AtomicReference to ensure thread-safety and visibility across parallel gets/sets
// https://stackoverflow.com/questions/14676997/java-memory-visibility-and-atomicreferences
// https://stackoverflow.com/questions/3964211/when-to-use-atomicreference-in-java
private final AtomicReference<T> value = new AtomicReference<>();
private final Supplier<T> defaultSupplier;
......@@ -68,15 +71,7 @@ public class DynamicConstant<T> {
// An exception will be thrown, and no data will be changed.
public T set(T newVal) {
Validatec.notNull(newVal, "Cannot set value to null");
// TODO Enhancement: Is there any danger that because set/get are not synchronized,
// the newVal memory reference can be written into value, and visible to other threads,
// even before the newVal object has completed its construction? Ie, the same reason why Java DCL fails?
// Because value is a AtomicReference, this should not be a problem?
// https://stackoverflow.com/questions/3964211/when-to-use-atomicreference-in-java
boolean updated = value.compareAndSet(null, newVal);
Validate.isTrue(updated || value.get().equals(newVal),
"New value: %s, does not match existing value: %s", newVal, value.get());
return newVal;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment