private static LocalBroadcastManager mInstance;
@NonNull
public static LocalBroadcastManager getInstance(@NonNull Context context) {
synchronized (mLock) {
if (mInstance == null) {
mInstance = new LocalBroadcastManager(context.getApplicationContext());
}
return mInstance;
}
}
系统下的这种单例 是安全的吗 感觉很乖
1
monkeylmj 2021 年 11 月 23 日
安全,就是效率低, 每次拿都要加锁。
|
2
rcj6056 OP 但是他这个 mInstance 没用 volatile 修饰 在 new 对象的时候会不会有可能导致重排 mInstance 为 null 的情况
|
4
DCELL 2021 年 11 月 23 日
我记得是这样的哇:
if null lock{ if null new() } return install |