com.payneteasy.superfly.common.singleton.SingletonHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of superfly-common Show documentation
Show all versions of superfly-common Show documentation
Common classes used by other Superfly libraries
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