Skip to content

Commit

Permalink
Merge pull request #1035 from mariofusco/q41314b
Browse files Browse the repository at this point in the history
Replace synchronized block with ReentrantLock in LazyFaultTolerance
  • Loading branch information
Ladicek committed Jul 12, 2024
2 parents 63dce35 + 377403c commit ef29958
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +10,8 @@ public final class LazyFaultTolerance<T> implements FaultTolerance<T> {
private final Supplier<FaultTolerance<T>> builder;
private final Class<?> asyncType;

private final ReentrantLock lock = new ReentrantLock();

private volatile FaultTolerance<T> instance;

LazyFaultTolerance(Supplier<FaultTolerance<T>> builder, Class<?> asyncType) {
Expand Down Expand Up @@ -38,12 +41,15 @@ public void run(Runnable action) {
private FaultTolerance<T> instance() {
FaultTolerance<T> 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;
Expand Down

0 comments on commit ef29958

Please sign in to comment.