From ff286508d637ea776255043fe386791a37450fca Mon Sep 17 00:00:00 2001 From: imperatorx Date: Mon, 3 Jul 2023 13:51:57 +0200 Subject: [PATCH] Made LazyValue virtual thread friendly --- .../main/java/io/quarkus/arc/impl/LazyValue.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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() {