锁的作用在于避免多线程同时访问某些公共资源的时候出错。
互斥锁(NSLock):互斥锁可以满足大部分情况,可以保证多线程访问公共资源时的互斥性。
递归锁(NSRecursiveLock):既然互斥锁能解决大部分问题,为什么还要有递归锁呢,问题在于互斥锁在同一线程多次上锁的时候会造成死锁,当同一线程中多次上锁的时候用递归锁。
条件锁(NSCondition):有时候我们会有这样的需求再一个线程中不满足条件的时候等待,如果条件满足再通知该线程继续执行,适用于经典的生产者和消费者场景中。举个例子:异步下载5张图片,下载完成后存储到本地。
- (void)downloadTest{
NSURLSession * session = [NSURLSession sharedSession];
//下载第一张图片
NSURLSessionDownloadTask * taskOne = [session downloadTaskWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self.conditionLock lock];
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
[self.dataArray addObject:data];
[self.conditionLock signal];
[self.conditionLock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[((UIImageView *)self.imageArray[0]) setImage:image];
});
}];
[taskOne resume];
//下载第二张图片
NSURLSessionDownloadTask * taskTwo = [session downloadTaskWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self.conditionLock lock];
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
[self.dataArray addObject:data];
[self.conditionLock signal];
[self.conditionLock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[((UIImageView *)self.imageArray[1]) setImage:image];
});
}];
[taskTwo resume];
//下载第三张图片
NSURLSessionDownloadTask * taskThree = [session downloadTaskWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self.conditionLock lock];
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
[self.dataArray addObject:data];
[self.conditionLock signal];
[self.conditionLock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[((UIImageView *)self.imageArray[2]) setImage:image];
});
}];
[taskThree resume];
//下载第四张图片
NSURLSessionDownloadTask * taskFour = [session downloadTaskWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self.conditionLock lock];
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
[self.dataArray addObject:data];
[self.conditionLock signal];
[self.conditionLock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[((UIImageView *)self.imageArray[3]) setImage:image];
});
}];
[taskFour resume];
//下载第五张图片
NSURLSessionDownloadTask * taskFive = [session downloadTaskWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self.conditionLock lock];
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
[self.dataArray addObject:data];
[self.conditionLock signal];
[self.conditionLock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[((UIImageView *)self.imageArray[4]) setImage:image];
});
}];
[taskFive resume];
[self storeData];
}
- (void) storeData{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.conditionLock lock];
while (self.dataArray.count != 5) {
[self.conditionLock wait];
}
dispatch_async(dispatch_get_main_queue(), ^{
//存储到本地
});
[self.conditionLock unlock];
});
}
锁保护了数组的写操作,且写过之后发送signal唤醒storeData部分wait部分继续判断当前下载状态。如果都下载完成执行写操作。
信号量:从另一个角度看信号量其实就是个有个特殊条件的条件锁(条件就是信号大于0)。更多用在控制多线程的最大并发量。