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

com.payneteasy.superfly.common.singleton.SingletonHolder Maven / Gradle / Ivy

There is a newer version: 1.7-32
Show newest version
package com.payneteasy.superfly.common.singleton;

/**
 * Utility class that holds a singleton instance. Its implementation prevents
 * a double-lock problem.
 * 
 * @author Roman Puchkovskiy
 * @param 
 */
public abstract class SingletonHolder {
    private volatile T instance;

    /**
     * Obtains a singleton instance held by this object.
     *
     * @return instance
     */
    public T getInstance() {
        if (instance == null) {
            synchronized (this) {
                if (instance == null) {
                    // this is to prevent double object creation if one thread
                    // blocks another one here
                    instance = createInstance();
                }
            }
        }
        return instance;
    }

    /**
     * Actually creates a new instance.
     *
     * @return created instance
     */
    protected abstract T createInstance();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy