static LOSingleton * shareInstance;
+( LOSingleton *)sharedInstance{
@synchronized(self){//这个东西其实就是 一个加锁。如果self 其他线程访问,则会阻塞。这样做一般是用来对单例 进行一个死锁的保护
if (shareInstance == nil) {
shareInstance = [[super allocWithZone:NULL] init];
}
}
return shareInstance;
}
//第二种方式
+ (LOSingleton *) sharedInstance
{
static LOSingleton *sharedInstance = nil ;
static dispatch_once_t onceToken; // 锁
dispatch_once (& onceToken, ^ { // 最多调用一次
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}