From 377403cc9d5d85ffa2473cd6f14632ef36209030 Mon Sep 17 00:00:00 2001 From: mariofusco Date: Fri, 12 Jul 2024 13:49:46 +0200 Subject: [PATCH] Replace synchronized block with ReentrantLock in LazyFaultTolerance --- .../faulttolerance/core/apiimpl/LazyFaultTolerance.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/implementation/core/src/main/java/io/smallrye/faulttolerance/core/apiimpl/LazyFaultTolerance.java b/implementation/core/src/main/java/io/smallrye/faulttolerance/core/apiimpl/LazyFaultTolerance.java index 69b90066..6d925e6f 100644 --- a/implementation/core/src/main/java/io/smallrye/faulttolerance/core/apiimpl/LazyFaultTolerance.java +++ b/implementation/core/src/main/java/io/smallrye/faulttolerance/core/apiimpl/LazyFaultTolerance.java @@ -1,6 +1,7 @@ package io.smallrye.faulttolerance.core.apiimpl; import java.util.concurrent.Callable; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; import io.smallrye.faulttolerance.api.FaultTolerance; @@ -9,6 +10,8 @@ public final class LazyFaultTolerance implements FaultTolerance { private final Supplier> builder; private final Class asyncType; + private final ReentrantLock lock = new ReentrantLock(); + private volatile FaultTolerance instance; LazyFaultTolerance(Supplier> builder, Class asyncType) { @@ -38,12 +41,15 @@ public void run(Runnable action) { private FaultTolerance instance() { FaultTolerance instance = this.instance; if (instance == null) { - synchronized (this) { + lock.lock(); + try { instance = this.instance; if (instance == null) { instance = builder.get(); this.instance = instance; } + } finally { + lock.unlock(); } } return instance;