单例模式双重校验有两种方式
1.使用synchronized
2.使用内部类 (推荐使用)
使用以下代码需简单修改
import com.harmonywisdom.ajproduct.ajproduct.mobile.bean.WarningInfoBean;
import java.util.ArrayList;
import java.util.List;
/**
* @Description 缓存处理
* @Author WangKun
* @Date 2019/12/18 18:09
* @Version
*/
public class SingletonCacheUtil {
// 缓存容器
private static List<WarningInfoBean> alertCache = new ArrayList<>();
// private volatile static SingletonCacheUtil instance = null;
//
// private SingletonCacheUtil() {}
// /**
// * @Description 单例双重检验模式
// * @throws
// * @Return
// * @Date 2019-12-19 11:17:56
// * @Author WangKun
// **/
// public static SingletonCacheUtil getInstance() {
// if (instance == null) {
// synchronized (SingletonCacheUtil.class) {
// if (instance == null) {
// instance = new SingletonCacheUtil();
// }
// }
// }
// return instance;
// }
private SingletonCacheUtil() {
}
/**
* @Description 使用内部类实现单例双重检验模式
* @throws
* @Return
* @Date 2019-12-19 11:17:56
* @Author WangKun
**/
private static class singleton {
private static SingletonCacheUtil singletonStatic = new SingletonCacheUtil();
}
/**
* @param
* @throws
* @Description 实例化
* @Return com.harmonywisdom.ajproduct.ajproduct.task.util.SingletonCacheUtil
* @Date 2019-12-19 11:18:09
* @Author WangKun
**/
public static SingletonCacheUtil getInstance() {
return singleton.singletonStatic;
}
/**
* @param
* @throws
* @Description 获取缓存
* @Return java.util.List<java.lang.Object>
* @Date 2019-12-19 11:17:34
* @Author WangKun
**/
public List<WarningInfoBean> getAlertCacheInfoList() {
return alertCache;
}
/**
* @param info
* @throws
* @Description 添加数据
* @Return void
* @Date 2019-12-19 11:20:54
* @Author WangKun
**/
public void addAlertCacheInfoList(WarningInfoBean info) {
alertCache.add(info);
}
/**
* @param
* @throws
* @Description 清楚缓存
* @Return void
* @Date 2019-12-19 11:22:10
* @Author WangKun
**/
public void clearCacheList() {
alertCache.clear();
}
}