iOS开发—线程安全
一、多线程的安全隐患
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
比如多个线程访问同一个对象、同一个变量、同一个文件
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题


问题代码:
YYViewController.m
05-线程安全
Created by apple on 14-6-23.
Copyright (c) 2014年 itcase. All rights reserved.
#import YYViewController.h
@interface YYViewController ()
@property(nonatomic,assign) leftTicketsCount;
@property(nonatomic,strong)NSThread *thread1;
@property(nonatomic,strong)NSThread *thread2;
@property(nonatomic,strong)NSThread *thread3;
@implementation YYViewController
- ()viewDidLoad
[super viewDidLoad];
默认有20张票
self.leftTicketsCount=
开启多个线程,模拟售票员售票
self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread1.name=
self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread2.name=
self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread3.name=
-()sellTickets
while ( 1.先检查票数
count=self.leftTicketsCount;
(count> 暂停一段时间
[NSThread sleepForTimeInterval:0.002
2.票数-1
self.leftTicketsCount= count-
获取当前线程
NSThread *current=[NSThread currentThread];
NSLog(%@--卖了一张票,还剩余%d张票,current,self.leftTicketsCount);
}
[NSThread exit];
-()touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[self.thread1 start];
[self.thread2 start];
[self.thread3 start];
打印结果:

二、安全隐患分析


三、如何解决
互斥锁使用格式
@synchronized(需要锁定的代码
锁定份代码只用把锁,用多把锁是无效的
代码示例:
YYViewController.m
05-线程安全
Created by apple on 14-6-23.
Copyright (c) 2014年 itcase. All rights reserved.
#import YYViewController.h
@interface YYViewController ()
@property(nonatomic,assign) leftTicketsCount;
@property(nonatomic,strong)NSThread *thread1;
@property(nonatomic,strong)NSThread *thread2;
@property(nonatomic,strong)NSThread *thread3;
@implementation YYViewController
- ()viewDidLoad
[super viewDidLoad];
默认有20张票
self.leftTicketsCount= 开启多个线程,模拟售票员售票
self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread1.name=
self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread2.name=
self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
self.thread3.name=
-()sellTickets
while ( @synchronized(self){只能加一把锁
1.先检查票数
count=self.leftTicketsCount;
(count> 暂停一段时间
[NSThread sleepForTimeInterval:0.002 2.票数-1
self.leftTicketsCount= count- 获取当前线程
NSThread *current=[NSThread currentThread];
NSLog(%@--卖了一张票,还剩余%d张票,current,self.leftTicketsCount);
}
[NSThread exit];
-()touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[self.thread1 start];
[self.thread2 start];
[self.thread3 start];
执行效果图

互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行任务
互斥锁,就是使用了线程同步技术
四:原子和非原子属性
OC在定义属性时有nonatomicatomic
atomic:原子属性,为setter方法加锁(默认就是atomic
nonatomic:非原子属性,不会为setter
atomic
@property (assign, atomic)
- ()setAge:(
@synchronized(self) {
_age = }
原子和非原子属性的选择
nonatomicatomic
atomic线程安全,需要消耗大量的资源
nonatomic非线程安全,适合内存小的移动设备
iOS开发的建议
所有属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力

浙公网安备 33010602011771号