synchronized 是一个递归锁,是使用的递归mutex来做同步
@synchronized 的作用是创建一个互斥锁,保证此时没有其它线程对self对象进行修改,保证代码的安全性。也就是包装这段代码是原子性的,安全的。这个是objective-c的一个锁定令牌,防止self对象在同一时间内被其他线程访问,起到保护线程安全的作用
比如:某个类Present中有个代理方法,如果多个类在调用此代理方法进行数据修改,就会有安全性问题,如果产生并发情况就会出问题,所以需要加个锁。
example1
@synchronized (obj) {
NSLog(@"1st sync");
@synchronized (obj) {
NSLog(@"2nd sync");
}
}
example2
@synchronized(nil)不起任何作用
example3
- (void)synchronizedAMethod {
@synchronized (self) {
//Safe
}
}
- (void)synchronizedBMethod {
@synchronized (self) {
//Safe
}
}
- (void)synchronizedCMethod {
@synchronized (self) {
//Safe
}
}
如果正在执行synchronizedAMethod 方法 则:synchronizedBMethod和synchronizedCMethod 需要等待前面的方法结束才能执行。
example4
NSObject *obj = [[NSObject alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@synchronized(self) {
NSLog(@"需要线程同步的操作1 开始");
sleep(3);
NSLog(@"需要线程同步的操作1 结束");
}
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(1);
@synchronized(obj) {
NSLog(@"需要线程同步的操作2");
}
});
需要线程同步的操作1 开始
需要线程同步的操作2
需要线程同步的操作1 结束
如果都是key 都是self 那么
需要线程同步的操作1 开始
需要线程同步的操作1 结束
需要线程同步的操作2
经典列子:卖票
两个售票员共享票的资源,如果两售票员都是各自卖自己的,没有统计对方卖了多少,只统计了自己卖的,那么统计的剩余票数就有问题。多线程共享资源就是这个问题,所以必须得保证共享资源的安全性。@synchronized就是这个作用
错误代码:
self.tickets = 100;
// 1.开启一条售票线程
NSThread * thread_1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
thread_1.name = @"售票 A";
[thread_1 start];
// 2.再开启一条售票线程
NSThread * thread_2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTickets) object:nil];
thread_2.name = @"售票 B";
[thread_2 start];
-(void)saleTickets{
while (YES) {
//1. 模拟延时
[NSThread sleepForTimeInterval:1];
//2. 判断是否还有票
if (self.tickets > 0) {
//3. 如果有票,卖一张,提示用户
self.tickets --;
NSLog(@"剩余票数%ld %@",(long)self.tickets,[NSThread currentThread]);
}else{
//4. 如果没票,退出循环
NSLog(@"没票了,来晚了 %@",[NSThread currentThread]);
break;
}
}
}
结果:

明显打印出来的情况是有问题的,两个线程都在卖票,这就表明saleTickets方法里的代码是不安全的,多线程是不安全的。这时就需要@synchronized来保证线程的安全
正确操作
while (YES) {
@synchronized(self){
[NSThread sleepForTimeInterval:1];
//1. 判断是否还有票
if (self.tickets > 0) {
//2. 如果还有票,卖一张,提示用户
self.tickets --;
NSLog(@"剩余票数 %ld %@",self.tickets,[NSThread currentThread]);
}else{
//3. 如果没有票,退出循环
NSLog(@"没票了,来晚了%@",[NSThread currentThread]);
break;
}
}
}
互斥锁优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量cpu资源
互斥锁使用前提:多条线程抢夺同一块资源
总结
synchronized中传入的object的内存地址,被用作key,通过hash map对应的一个系统维护的递归锁。
如果object 被外部访问变化,则就失去了锁的作用。所以最好本类声明一个对象属性来当做key
链接:http://mrpeak.cn/blog/synchronized/
网友评论