ios开发多线程篇——线程安全
2016-04-20 10:12
405 查看
一.多线程的安全隐患
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源。
比如多个线程访问同一个对象,同一个变量,同一个文件。
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题。
示例一:
示例二:
问题代码:
二.如何解决
互斥锁使用格式
@synchroized(锁对象){//需要锁定的代码}
注意:锁定1份代码只用1把锁,用多把锁是无效的
代码示例:
互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行
互斥锁,就是使用了线程同步技术
四.原子和非原子属性
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
atomic加锁原理
原子和非原子属性的选择
nonatomic和atomic对比
atomic:线程安全,需要消耗大量的资源。
nonatomic:非线程安全,适合内存小的移动设备
ios开发的建议
所有的属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将枷锁,资源抢夺的业务逻辑交给服务器端处理
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源。
比如多个线程访问同一个对象,同一个变量,同一个文件。
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题。
示例一:
示例二:
问题代码:
#import "ViewController.h" @interface ViewController () //剩余票数 @property(nonatomic,assign) int leftTicketsCount; @property(nonatomic,strong) NSThread *thread1; @property(nonatomic,strong) NSThread *thread2; @property(nonatomic,strong) NSThread *thread3; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //默认有10张票 self.leftTicketsCount = 10; self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员A"]; self.thread1.name = @"售票员A"; self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员B"]; self.thread2.name = @"售票员B"; self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员C"]; self.thread3.name = @"售票员C"; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)sellTickets{ while (1) { //先检查票数 int count = self.leftTicketsCount; if (count > 0) { //暂停一段时间 [NSThread sleepForTimeInterval:0.02]; //票数-1 self.leftTicketsCount = count - 1; //获取当前线程 NSThread *current = [NSThread currentThread]; NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount); } else{ //退出线程 [NSThread exit]; } } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //开启线程 [self.thread1 start]; [self.thread2 start]; [self.thread3 start]; } @end
二.如何解决
互斥锁使用格式
@synchroized(锁对象){//需要锁定的代码}
注意:锁定1份代码只用1把锁,用多把锁是无效的
代码示例:
#import "ViewController.h" @interface ViewController () //剩余票数 @property(nonatomic,assign) int leftTicketsCount; @property(nonatomic,strong) NSThread *thread1; @property(nonatomic,strong) NSThread *thread2; @property(nonatomic,strong) NSThread *thread3; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //默认有10张票 self.leftTicketsCount = 10; self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员A"]; self.thread1.name = @"售票员A"; self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员B"]; self.thread2.name = @"售票员B"; self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTickets) object:@"售票员C"]; self.thread3.name = @"售票员C"; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)sellTickets{ while (1) { @synchronized (self) { //先检查票数 int count = self.leftTicketsCount; if (count > 0) { //暂停一段时间 [NSThread sleepForTimeInterval:0.02]; //票数-1 self.leftTicketsCount = count - 1; //获取当前线程 NSThread *current = [NSThread currentThread]; NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount); } else{ //退出线程 [NSThread exit]; } } } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //开启线程 [self.thread1 start]; [self.thread2 start]; [self.thread3 start]; } @end
互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行
互斥锁,就是使用了线程同步技术
四.原子和非原子属性
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
atomic加锁原理
@property(assign,atomic) int age; - (void)setAge:(int)age{ @synchroinzed(self){ _age = age; } }
原子和非原子属性的选择
nonatomic和atomic对比
atomic:线程安全,需要消耗大量的资源。
nonatomic:非线程安全,适合内存小的移动设备
ios开发的建议
所有的属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将枷锁,资源抢夺的业务逻辑交给服务器端处理
相关文章推荐
- IOS开发之——自定义导航控制器
- iOS NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- 欢迎使用CSDN-markdown编辑器
- iOS 直播技术平台
- 关于键盘上方创建返回按钮
- 极验geetest IOS端开发文档
- iOS 处理图片的一些小 Tip
- 初识 iOS 9 中新的联系人框架
- iOS 开发多线程及其进程和线程的介绍和理解
- iOS 类别和扩展(Categories和Extensions)
- iOS中的小知识点
- iOS 数据缓存
- iOS选中文字点击learn崩溃
- ios常用三方资源
- 【代码笔记】iOS-利用图片序列创建动态图片效果
- iOS巅峰之跑马灯
- ios,使用CocoaPods做第三方库的依赖管理
- iosGCD其一
- Cordova - 使用Cordova开发iOS应用实战4(调用摄像头拍照,并编辑)
- IOS开发基础知识--碎片37