diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyValue.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyValue.java index 5c383b25516eb..a462525b92eee 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyValue.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyValue.java @@ -1,5 +1,7 @@ package io.quarkus.arc.impl; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; /** @@ -10,6 +12,8 @@ public class LazyValue { private final Supplier supplier; + private final Lock lock = new ReentrantLock(); + private transient volatile T value; public LazyValue(Supplier supplier) { @@ -21,11 +25,15 @@ public T get() { if (valueCopy != null) { return valueCopy; } - synchronized (this) { + + lock.lock(); + try { if (value == null) { value = supplier.get(); } return value; + } finally { + lock.unlock(); } } @@ -34,9 +42,9 @@ public T getIfPresent() { } public void clear() { - synchronized (this) { - value = null; - } + lock.lock(); + value = null; + lock.unlock(); } public boolean isSet() {