Skip to content

Commit 3a3f0be

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 f61e8d2 commit 3a3f0be

File tree

1 file changed

+4
-7
lines changed
  • src/main/java/org/springframework/data/util

1 file changed

+4
-7
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Oliver Gierke
3636
* @author Mark Paluch
37+
* @author Henning Rohlfs
3738
* @since 2.0
3839
*/
3940
@RequiredArgsConstructor
@@ -45,7 +46,7 @@ public class Lazy<T> implements Supplier<T> {
4546

4647
private final Supplier<? extends T> supplier;
4748
private @Nullable T value = null;
48-
private boolean resolved = false;
49+
private volatile boolean resolved = false;
4950

5051
/**
5152
* Creates a new {@link Lazy} to produce an object lazily.
@@ -203,15 +204,11 @@ public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
203204
@Nullable
204205
public T getNullable() {
205206

206-
T value = this.value;
207-
208-
if (this.resolved) {
207+
if (resolved) {
209208
return value;
210209
}
211210

212-
value = supplier.get();
213-
214-
this.value = value;
211+
this.value = supplier.get();
215212
this.resolved = true;
216213

217214
return value;

0 commit comments

Comments
 (0)