private static volatile SettingsDbHelper sInst = null; // 这里添加了 volatile
public static SettingsDbHelper getInstance(Context context) {
SettingsDbHelper inst = sInst; // 在这里创建临时变量
if (inst == null) {
synchronized (SettingsDbHelper.class) {
inst = sInst;
if (inst == null) {
inst = new SettingsDbHelper(context);
sInst = inst;
}
}
}
return inst; // 注意这里返回的是临时变量
}
单例模式优化
最新推荐文章于 2022-09-05 19:44:26 发布