gu.simplemq.utils.ILazyInitVariable Maven / Gradle / Ivy
package gu.simplemq.utils;
import com.google.common.base.Supplier;
import static com.google.common.base.Preconditions.checkNotNull;;
/**
* 延迟初始化(Lazy initialization)变量封装接口
*
* @author guyadong
*
* @param 延迟变量类型
*/
public interface ILazyInitVariable {
/**
* 返回延迟初始化的 T 实例
*
* @return
*/
public T get();
public static class Factory {
/**
* 创建 {@link TlsImpl}实例
* @param supplier T 实例提供者
* @return
*/
public static final ILazyInitVariable makeInstance(final Supplier supplier){
return makeInstance(false,supplier);
}
/**
* 创建{@link ILazyInitVariable}实例
* @param tls 为{@code true}创建 {@link TlsImpl}实例,否则创建{@link VolatileImpl}实例
* @param supplier T 实例提供者
* @return
*/
public static final ILazyInitVariable makeInstance(boolean tls,final Supplier supplier) {
return tls
? new TlsImpl(supplier)
: new VolatileImpl(supplier);
}
/**
* 基于volatile的双重检查锁定实现{@link ILazyInitVariable}接口
* 原理说明参见《The
* "Double-Checked Locking is Broken" Declaration》
* 要求 JDK5 以上版本
*
* @param variable type
*/
private static class VolatileImpl implements ILazyInitVariable {
private volatile T var = null;
private final Supplier supplier;
public VolatileImpl(Supplier supplier) {
this.supplier = checkNotNull(supplier);
}
@Override
public T get() {
// Double-checked locking
if (null == var) {
synchronized (this) {
if (null == var) {
var = supplier.get();
}
}
}
return var;
}
}
/**
* 基于Thread Local Storage的双重检查锁定实现{@link ILazyInitVariable}接口
* 原理说明参见《The "Double-Checked Locking is Broken" Declaration》
*
* @param variable type
*/
private static class TlsImpl implements ILazyInitVariable {
/**
* If perThreadInstance.get() returns a non-null value, this thread has done
* synchronization needed to see initialization of helper
*/
@SuppressWarnings("rawtypes")
private final ThreadLocal perThreadInstance = new ThreadLocal();
private T var = null;
private final Supplier supplier;
public TlsImpl(Supplier supplier) {
this.supplier = checkNotNull(supplier);
}
@Override
public T get() {
if (null == perThreadInstance.get()) {
init();
}
return var;
}
@SuppressWarnings({ "unchecked" })
private void init() {
synchronized (this) {
if (null == var) {
var = supplier.get();
}
}
// Any non-null value would do as the argument here
perThreadInstance.set(perThreadInstance);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy