reschke commented on PR #2457:
URL: https://github.com/apache/jackrabbit-oak/pull/2457#issuecomment-3204186131

   > I found this alternative solution with an AtomicReference here: 
https://www.baeldung.com/java-lambda-lazy-field-initialization#thread-safe-solution
   > 
   > ```
   >     private final AtomicReference<T> data;
   > 
   >     public LazyLambdaThreadSafeSupplier(Supplier<T> expensiveData) {
   >         super(expensiveData);
   >         data = new AtomicReference<>();
   >     }
   > 
   >     public T getData() {
   >         if (data.get() == null) {
   >             synchronized (data) {
   >                 if (data.get() == null) {
   >                     data.set(expensiveData.get());
   >                 }
   >             }
   >         }
   >         return data.get();
   >     }
   > ```
   > 
   > Seems to be a bit cleaner than the current solution: only a single 
AtomicReference variable instead of two variables, no need for a volatile 
boolean guard variable as that synchronization happens inside the 
AtomicReference. I find it more elegant and easier to understand. But it's just 
an idea for consideration, as the current solution also seems to be correct.
   
   I *believe* this would have the same issue wrt not returning from the 
synchronized block, at which time it gets more complex, too.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: oak-dev-unsubscr...@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to