Skip to content

Commit a442b90

Browse files
xehonkodrotbohm
authored andcommitted
DATACMNS-1806 - Prevent returning null from Lazy due to concurrency problem.
When accessed concurrently from multiple threads, Lazy.getNullable() could return null unexpectedly. Original pull request: #468.
1 parent d10560f commit a442b90

File tree

1 file changed

+5
-8
lines changed
  • src/main/java/org/springframework/data/util

1 file changed

+5
-8
lines changed

src/main/java/org/springframework/data/util/Lazy.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Oliver Gierke
3232
* @author Mark Paluch
33+
* @author Henning Rohlfs
3334
* @since 2.0
3435
*/
3536
public class Lazy<T> implements Supplier<T> {
@@ -39,7 +40,7 @@ public class Lazy<T> implements Supplier<T> {
3940
private final Supplier<? extends T> supplier;
4041

4142
private @Nullable T value;
42-
private boolean resolved;
43+
private volatile boolean resolved;
4344

4445
/**
4546
* Creates a new {@link Lazy} instance for the given supplier.
@@ -222,18 +223,14 @@ public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
222223
@Nullable
223224
public T getNullable() {
224225

225-
T value = this.value;
226-
227226
if (this.resolved) {
228-
return value;
227+
return this.value;
229228
}
230229

231-
value = supplier.get();
232-
233-
this.value = value;
230+
this.value = supplier.get();
234231
this.resolved = true;
235232

236-
return value;
233+
return this.value;
237234
}
238235

239236
/*

0 commit comments

Comments
 (0)