All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.util.InterThreadMemoizedSupplier Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.util;

import graphql.Internal;

import java.util.function.Supplier;

/**
 * This memoizing supplier DOES use locked double locking to set its value.
 *
 * @param  for two
 */
@Internal
public class InterThreadMemoizedSupplier implements Supplier {

    private final Supplier delegate;
    private volatile boolean initialized;
    private final LockKit.ReentrantLock lock = new LockKit.ReentrantLock();
    private T value;

    public InterThreadMemoizedSupplier(Supplier delegate) {
        this.delegate = delegate;
    }


    @Override
    public T get() {
        if (!initialized) {
            lock.lock();
            try {
                if (initialized) {
                    return value;
                }
                value = delegate.get();
                initialized = true;
                return value;
            } finally {
                lock.unlock();
            }
        }
        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy