1.Dispatch Group
在追加到 Dispatch Queue 中的多个任务处理完毕之后想执行结束处理,这种需求会经常出现。如果只是使用一个 Serial Dispatch Queue (串行队列)时,只想要将执行的处理全部追加到改串行队列中并在最后追加结束处理即可,但是在使用 Concurrent Queue 时,可能会同时使用多个 Dispatch Queue 时,源码就会变得很复杂。
这种情况下,就可以使用 Dispatch Group。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| dispatch_group_t group = dispatch_group_create() dispatch_queue_t queue = dispatch_queue_create("com.wangwang.www", DISPATCH_QUEUE_CONCURRENT) dispatch_group_async(group, queue, ^{ for (int i = 0 if (i == 999) { NSLog(@"11111") } } }) dispatch_group_async(group, queue, ^{ NSLog(@"22222222") }) dispatch_group_async(group, queue, ^{ NSLog(@"3333") })
dispatch_group_notify(group, queue, ^{ NSLog(@"done") })
|
因为想 Concurrent Dispatch Queue 追加处理,多个线程并行执行,所以追加处理的执行顺序不定。执行顺序会发生变化,但是执行结果的 done 一定是最后输出的。
无论向什么样的 Dispatch Queue 中追加处理,使用 Dispatch Group 都可以监视这些处理执行的结果。一旦监测到所有的处理执行结束,就可以将结束的处理追加到 Dispatch Queue 中,这就是使用 Dispatch Group 的原因。
下面是一个使用 Dispatch Group 异步下载两张图片,然后合并成一张图片的 demo。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #import "ViewController.h"
@interface ViewController () @property(nonatomic, strong) UIImage *imageOne; @property(nonatomic, strong) UIImage *imageTwo; @property(nonatomic, weak) UILabel *textLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self operation1]; } - (void)operation1 { UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 450, 0, 0)]; textLabel.text = @"正在下载图片"; [textLabel sizeToFit]; [self.view addSubview:textLabel]; self.textLabel = textLabel; [self group]; NSLog(@"在下载图片的时候,主线程貌似还可以干点什么"); } - (void)group { UIImageView *imageView = [[UIImageView alloc] init]; [self.view addSubview:imageView]; dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_queue_create( "cn.gcd-group.www", DISPATCH_QUEUE_CONCURRENT); dispatch_group_async(group, queue, ^{ NSLog(@"正在下载第一张图片"); NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"[http://images2015.cnblogs.com/blog/471463/201509/471463-20150912213125372-589808688.png](http://images2015.cnblogs.com/blog/471463/201509/471463-20150912213125372-589808688.png)" ]]; NSLog(@"第一张图片下载完毕"); self.imageOne = [UIImage imageWithData:data]; }); dispatch_group_async(group, queue, ^{ NSLog(@"正在下载第二张图片"); NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@[http: NSLog(@"第二张图片下载完毕"); self.imageTwo = [UIImage imageWithData:data]; }); dispatch_group_notify(group, queue, ^{ UIGraphicsBeginImageContext(CGSizeMake(300, 400)); [self.imageOne drawInRect:CGRectMake(0, 0, 150, 400)]; [self.imageTwo drawInRect:CGRectMake(150, 0, 150, 400)]; UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_async(dispatch_get_main_queue(), ^{ UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage]; [self.view addSubview:imageView]; self.textLabel.text = @"图片合并完毕"; }); }); } @end
|
2. Dispatch_barrier_async
在访问数据库或者文件的时候,我们可以使用Serial Dispatch Queue可避免数据竞争问题,代码如下所示:
其实使用GCD可以简单高效的代替同步块或者锁对象,可以使用,串行同步队列,将读操作以及写操作都安排在同一个队列里,即可保证数据同步,代码如下:
多个getter方法(也就是读取)是可以并发执行的,而getter(读)与setter(写)方法是不能并发执行的,利用这个特点,还能写出更快的代码来,这次注意,不用串行队列,而改用并行队列:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #import <Foundation/Foundation.h> @interface ZYPerson : NSObject @property (nonatomic, copy) NSString *name; @end #import "ZYPerson.h" @interface ZYPerson () @end static NSString *_name; static dispatch_queue_t _concurrentQueue; @implementation ZYPerson - (instancetype)init { if (self = [super init]) { _concurrentQueue = dispatch_queue_create("com.person.syncQueue" , DISPATCH_QUEUE_CONCURRENT); } return self; } - (void)setName:(NSString *)name { dispatch_barrier_async(_concurrentQueue, ^{ _name = [name copy ]; }); } - (NSString *)name { __block NSString *tempName; dispatch_sync(_concurrentQueue, ^{ tempName = _name; }); return tempName; } @end
|
这样优化,可以发现这种做法肯定比使用串行队列要快。
在这个代码中,我用了点新的东西,dispatch_barrier_async,可以翻译成栅栏(barrier),它可以往队列里面发送任务(块,也就是block),这个任务有栅栏(barrier)的作用。
在队列中,barrier块必须单独执行,不能与其他block并行。这只对并发队列有意义,并发队列如果发现接下来要执行的block是个barrier block,那么就一直要等到当前所有并发的block都执行完毕,才会单独执行这个barrier block代码块,等到这个barrier block执行完毕,再继续正常处理其他并发block。在上面的代码中,setter方法中使用了barrier block以后,对象的读取操作依然是可以并发执行的,但是写入操作就必须单独执行了。