You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/08/31 07:16:27 UTC

[isis] 02/03: ISIS-1841: Internal API: add a thread-safe variant of _Lacy

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit fe8542e765afc05ef3e5d1210c0b8afeb732a91c
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Aug 31 08:55:01 2018 +0200

    ISIS-1841: Internal API: add a thread-safe variant of _Lacy
---
 .../apache/isis/commons/internal/base/_Lazy.java   |  8 ++++-
 .../base/{_Lazy.java => _LazyThreadSafe.java}      | 37 +++++++++++++++-------
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java
index f3ba221..18eb150 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java
@@ -26,7 +26,7 @@ import java.util.function.Supplier;
 /**
  * <h1>- internal use only -</h1>
  * <p>
- * Supplier with memoization.
+ * (non-thread-safe) Supplier with memoization.
  * </p>
  * <p>
  * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
@@ -41,6 +41,12 @@ public final class _Lazy<T> implements Supplier<T> {
     private T value;
     private boolean memoized;
 
+    /**
+     * Concurrent calls to this lazy's get() method might result in concurrent calls to the 
+     * specified {@code supplier}. 
+     * @param supplier
+     * @return an (non-thread-safe) instance of _Lacy that initializes with the specified {@code supplier}
+     */
     public static <T> _Lazy<T> of(Supplier<? extends T> supplier) {
         return new _Lazy<T>(supplier);
     }
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_LazyThreadSafe.java
similarity index 65%
copy from core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java
copy to core/commons/src/main/java/org/apache/isis/commons/internal/base/_LazyThreadSafe.java
index f3ba221..436cd36 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Lazy.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_LazyThreadSafe.java
@@ -26,7 +26,7 @@ import java.util.function.Supplier;
 /**
  * <h1>- internal use only -</h1>
  * <p>
- * Supplier with memoization.
+ * Thread-safe Supplier with memoization.
  * </p>
  * <p>
  * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
@@ -35,17 +35,24 @@ import java.util.function.Supplier;
  *
  * @since 2.0.0
  */
-public final class _Lazy<T> implements Supplier<T> {
+public final class _LazyThreadSafe<T> implements Supplier<T> {
 
     private final Supplier<? extends T> supplier;
     private T value;
     private boolean memoized;
 
-    public static <T> _Lazy<T> of(Supplier<? extends T> supplier) {
-        return new _Lazy<T>(supplier);
+    /**
+     * Thread-safe variant to {@link _Lazy#of(Supplier)}.
+     * Concurrent calls to this lazy's get() method will never result in concurrent calls to the 
+     * specified {@code supplier}. 
+     * @param supplier
+     * @return an (thread-safe) instance of _Lacy that initializes with the specified {@code supplier}
+     */
+    public static <T> _LazyThreadSafe<T> of(Supplier<? extends T> supplier) {
+        return new _LazyThreadSafe<T>(supplier);
     }
 
-    private _Lazy(Supplier<? extends T> supplier) {
+    private _LazyThreadSafe(Supplier<? extends T> supplier) {
         this.supplier = requires(supplier, "supplier");
     }
 
@@ -53,7 +60,9 @@ public final class _Lazy<T> implements Supplier<T> {
      * @return whether this lazy got initialized and holds a memoized value
      */
     public boolean isMemoized() {
-        return memoized;
+        synchronized (this) {
+            return memoized;    
+        }
     }
 
     /**
@@ -62,8 +71,10 @@ public final class _Lazy<T> implements Supplier<T> {
      *
      */
     public void clear() {
-        this.memoized = false;
-        this.value = null;
+        synchronized (this) {
+            this.memoized = false;
+            this.value = null;
+        }
     }
 
     /**
@@ -72,11 +83,13 @@ public final class _Lazy<T> implements Supplier<T> {
      */
     @Override
     public T get() {
-        if(memoized) {
-            return value;
+        synchronized (this) {
+            if(memoized) {
+                return value;
+            }
+            memoized=true;
+            return value = supplier.get();    
         }
-        memoized=true;
-        return value = supplier.get();
     }
 
 }