Skip to content
Prev Previous commit
Next Next commit
Make LazyEvaluator thread-safe
  • Loading branch information
markushi committed Oct 8, 2024
commit c8ea8098790f1b767a904d34adf7dcb95f8fde37
13 changes: 10 additions & 3 deletions sentry/src/main/java/io/sentry/util/LazyEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
@ApiStatus.Internal
public final class LazyEvaluator<T> {
private @Nullable T value = null;

private volatile @Nullable T value = null;
private final @NotNull Evaluator<T> evaluator;

/**
Expand All @@ -28,10 +29,16 @@ public LazyEvaluator(final @NotNull Evaluator<T> evaluator) {
*
* @return The result of the evaluator function.
*/
public synchronized @NotNull T getValue() {
public @NotNull T getValue() {
if (value == null) {
value = evaluator.evaluate();
synchronized (this) {
if (value == null) {
value = evaluator.evaluate();
}
}
}

//noinspection DataFlowIssue
return value;
}

Expand Down