Skip to content

Commit

Permalink
Merge pull request #34479 from imperatorx/virtual-thread-friendly-laz…
Browse files Browse the repository at this point in the history
…yvalue

Made LazyValue virtual thread friendly
  • Loading branch information
geoand authored Jul 26, 2023
2 parents 747cc8e + ff28650 commit 0e89470
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -10,6 +12,8 @@ public class LazyValue<T> {

private final Supplier<T> supplier;

private final Lock lock = new ReentrantLock();

private transient volatile T value;

public LazyValue(Supplier<T> supplier) {
Expand All @@ -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();
}
}

Expand All @@ -34,9 +42,9 @@ public T getIfPresent() {
}

public void clear() {
synchronized (this) {
value = null;
}
lock.lock();
value = null;
lock.unlock();
}

public boolean isSet() {
Expand Down

0 comments on commit 0e89470

Please sign in to comment.