ios 线程安全单例写法

#import "DemoObj.h"

@implementation DemoObj

static DemoObj *instance;

/**

1. 重写allocWithZone,用dispatch_once实例化一个静态变量

2. 写一个+sharedXXX方便其他类调用

*/

// 在iOS中,所有对象的内存空间的分配,最终都会调用allocWithZone方法

// 如果要做单例,需要重写此方法

// GCD提供了一个方法,专门用来创建单例的

+ (id)allocWithZone:(struct _NSZone *)zone

{

static DemoObj *instance;

// dispatch_once是线程安全的,onceToken默认为0

static dispatch_once_t onceToken;

// dispatch_once宏可以保证块代码中的指令只被执行一次

dispatch_once(&onceToken, ^{

// 在多线程环境下,永远只会被执行一次,instance只会被实例化一次

instance = [super allocWithZone:zone];

});

return instance;

}

+ (instancetype)sharedDemoObj

{

return [[self alloc] init];

}

@end

时间: 2024-08-29 06:37:58

ios 线程安全单例写法的相关文章

单例写法 转

如何正确地写出单例模式 1.懒汉式,线程不安全 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题.当有多个线程并行调用 getInstance() 的时候,就会创建多个实例.也就是说在多线程下不能正常工作 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == nu

有一鲜为人知的单例写法-ThreadLocal

还有一鲜为人知的单例写法-ThreadLocal 源码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们平常用双重检查锁很不一样.而是用来一个ThreadLocal,这个也可以实现单例啊,那这个与双重检查锁实现的单例有什么区别呢? 1.FocusFinder /** * The algorithm used for finding the next focusable view in a given direction * from a view

另一鲜为人知的单例写法-ThreadLocal

另一鲜为人知的单例写法-ThreadLocal 源代码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们寻经常使用双重检查锁非常不一样.而是用来一个ThreadLocal.这个也能够实现单例啊,那这个与双重检查锁实现的单例有什么差别呢? 1.FocusFinder /** * The algorithm used for finding the next focusable view in a given direction * from a v

IOS开发模式——单例

单例的模式在网上有很多,今天发下我个人对单例模式的理解.整个app中只存在一个实例,也只会进行一次实例,在实例完成之后是不可以人释放的(当App关闭之后,等系统自己回收). 也就是说,如果我们写得某个类符合了上述条件,那么我们也可以称这个类为单例. 在非ARC的工程中,我们需要针对alloc,retain,copy等会增加retaincount的参数加以控制,对release和autorelease等减少retailcount的操作增加控制,以确保单一实例,绝不释放. 在ARC的工厂中,由于,内

iOS Dev (67) 单例的实现

iOS Dev (67) 单例的实现 博客:http://blog.csdn.net/prevention 作者:大锐哥 摘自:Learn iPhone and iPad cocos2d Game Development 实现 在 .m 文件里 static MyManager *sharedManager = nil; +(MyManager*) sharedManager { if (sharedManager == nil) { sharedManager = [[MyManager al

Egret中的三种单例写法

1 普通的单例写法 class Single{ private static instance:Single; public static getInstance():Single{ if(this.instance == null){ this.instance = new Single(); } return this.instance; } public run(){ } } Single.getInstance().run(); 2 Module写法.仿照的Egret中Res资源类写法.

Swift百万线程攻破单例(Singleton)模式

一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared: SwiftSingleton { if !Inner.instance { Inner.instance = SwiftSingleton() } return Inner.instance! } struct Inner { static var instance: SwiftSingleto

java线程:单例隐藏ThreadLocal实现线程数据共享

问题: 给定的二叉查找树中,有两个节点不小心被调换了位置,现在需要将其修正,不改变树的结构. 分析: 二叉排序树的中序遍历是有序的,所以这个问题又是建立在中序遍历模板上的问题,所以我们可以对其进行中序遍历,并用一个pre指针指向当前遍历结果中的最后一个结点,即下次遍历前的前一个结点.然后就可以通过将当前结点与pre结点进行比较,来判断是否有序了.若乱序,就将这两个结点都放入到预先定义的容器中. 错误的形式就这两种,我们看到,在乱序容器中,最多就存了四个元素,所以空间复杂度还是满足O(n)的,当然

性能比较好的单例写法

2019/10/27, .Net c#代码片段 摘要:一种性能比较好的单例写法 参考来源 其他单例思路: 1.使用依赖注入,注册为单例模式 2.使用双重锁机制 public sealed class SingletonBase//应该使用密封类防止派生 { //写单例的方法 //public string Getxxx(){ } private SingletonBase() { } public static SingletonBase Instance { get { return Nest