这篇文章介绍了GCD的使用,如果对事例输出结果有疑惑不是很懂的话,请看这篇文章,会原理了使用起来就有底气了。(http://www.jianshu.com/p/5840523fb3ea)
1.任务的创建方法
// 同步执行任务
dispatch_sync(queue1, ^{
NSLog(@"%@", [NSThread currentThread]); // 这里放具体的任务代码
});
// 异步执行任务
dispatch_async(queue1, ^{
NSLog(@"%@", [NSThread currentThread]); // 这里放具体的任务代码
});
2.队列的创建方法
// 串行队列的创建方法
dispatch_queue_t queue1 = dispatch_queue_create("test1.queue", DISPATCH_QUEUE_SERIAL);
// 并行队列的创建方法
dispatch_queue_t queue2 = dispatch_queue_create("test2.queue", DISPATCH_QUEUE_CONCURRENT);
// 获取全局并发队列
dispatch_queue_t queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 获取主队列(如:渲染UI等操作都必须在主队列中执行)
dispatch_queue_t mainQueue = dispatch_get_main_queue();
- 使用
dispatch_queue_create
创建对象,第一个参数表示队列的唯一标识符,用于debug,可为空;第二个参数用来指定是串行队列还是并发队列。DISPATCH_QUEUE_SERIAL
表示是串行队列,DISPATCH_QUEUE_CONCURRENT
表示是并发队列。 - 并发队列还可以使用
dipatch_get_global_queue
来创建全局并发队列。GCD默认提供了全局的并发队列,需要传入两个参数。第一个参数表示队列的优先级,一般用DISPATCH_QUEUE_PRIORITY_DEFAULT
。第二个参数传0即可。 - 一个app运行都有且只有一个主队列,如界面渲染等操作都是要回到主队列去执行的。
3.GCD的基本使用方法
1.同步执行 + 并发队列
2.同步执行 + 串行队列
3.同步执行 + 主队列
4.异步执行 + 并发队列(最常用)
5.异步执行 + 串行队列
6.异步执行 + 主队列
并发队列 | 串行队列 | 主队列 | |
---|---|---|---|
同步(sync) | 没有开启新线程,串行执行任务 | 没有开启新线程,串行执行任务 | 没有开启新线程,串行执行任务 |
异步(async) | 有开启新线程,并发执行任务 | 有开启新线程(1条),串行执行任务 | 没有开启新线程,串行执行任务 |
1.同步执行 + 并发队列
不会开启新的线程,执行完一个任务再执行下一个任务
// 同步 + 并行
- (void)syncConcurrent
{
NSLog(@"start......");
dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end......");
}
对应的输出:
2016-11-07 20:39:55.815238 GCDTest[62422:2294460] start......
2016-11-07 20:39:55.819010 GCDTest[62422:2294460] 1------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821131 GCDTest[62422:2294460] 1------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821686 GCDTest[62422:2294460] 2------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821716 GCDTest[62422:2294460] 2------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821739 GCDTest[62422:2294460] 3------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821760 GCDTest[62422:2294460] 3------<NSThread: 0x100201d90>{number = 1, name = main}
2016-11-07 20:39:55.821776 GCDTest[62422:2294460] end......
从输出结果可以看出,并没有创建新的线程,虽然是并发队列,然而依然是在当前线程中按顺序执行。
2. 同步执行 + 串行队列
不会开启新线程,在当前线程执行任务。执行完一个任务再执行下一个任务。
// 同步 + 串行
- (void)syncSerial
{
NSLog(@"begin.......");
dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end.......");
}
对应输出结果:
2016-11-07 20:53:40.087101 GCDTest[63292:2342125] begin.......
2016-11-07 20:53:40.088562 GCDTest[63292:2342125] 1------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088631 GCDTest[63292:2342125] 1------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088670 GCDTest[63292:2342125] 2------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088699 GCDTest[63292:2342125] 2------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088717 GCDTest[63292:2342125] 3------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088732 GCDTest[63292:2342125] 3------<NSThread: 0x1003009e0>{number = 1, name = main}
2016-11-07 20:53:40.088741 GCDTest[63292:2342125] end.......
3. 同步执行 + 主队列
在主线程中调用会出现线程卡死。
// 同步 + 主线程 (这里的syncMain方法假设是在主线程中执行的)
- (void)syncMain
{
NSLog(@"begin.......");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end.......");
}
对应的输出:
2016-11-07 21:00:42.449770 GCDTest[63753:2366743] begin.......
这里出现了线程死锁,至于为什么出现死锁,请看这篇文章: http://www.jianshu.com/p/5840523fb3ea
4. 异步执行 + 并发队列 (常用)
可同时开启多线程,任务交替执行
// 异步 + 并发
- (void)asyncConcurrent
{
NSLog(@"begin.......");
dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end.......");
}
输出结果:
2016-11-07 21:57:29.792065 GCDTest[67516:2568538] begin.......
2016-11-07 21:57:29.793060 GCDTest[67516:2568538] end.......
2016-11-07 21:57:29.793827 GCDTest[67516:2568759] 1------<NSThread: 0x100300180>{number = 2, name = (null)}
2016-11-07 21:57:29.794795 GCDTest[67516:2568761] 3------<NSThread: 0x100401fb0>{number = 3, name = (null)}
2016-11-07 21:57:29.794928 GCDTest[67516:2568759] 1------<NSThread: 0x100300180>{number = 2, name = (null)}
2016-11-07 21:57:29.795082 GCDTest[67516:2568761] 3------<NSThread: 0x100401fb0>{number = 3, name = (null)}
2016-11-07 21:57:29.796385 GCDTest[67516:2568760] 2------<NSThread: 0x1002052c0>{number = 4, name = (null)}
2016-11-07 21:57:29.796430 GCDTest[67516:2568760] 2------<NSThread: 0x1002052c0>{number = 4, name = (null)}
5. 异步执行 + 串行队列
会开启新线程,串行队列中的任务会在新线程中按顺序执行
// 异步 + 串行
- (void)asyncSerial
{
NSLog(@"begin.......");
dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end.......");
}
输出结果:
2016-11-07 22:13:45.792144 GCDTest[68539:2627655] begin.......
2016-11-07 22:13:45.793360 GCDTest[68539:2627655] end.......
2016-11-07 22:13:45.794873 GCDTest[68539:2627680] 1------<NSThread: 0x100203c10>{number = 2, name = (null)}
2016-11-07 22:13:45.794943 GCDTest[68539:2627680] 1------<NSThread: 0x100203c10>{number = 2, name = (null)}
2016-11-07 22:13:45.794986 GCDTest[68539:2627680] 2------<NSThread: 0x100203c10>{number = 2, name = (null)}
2016-11-07 22:13:45.795008 GCDTest[68539:2627680] 2------<NSThread: 0x100203c10>{number = 2, name = (null)}
2016-11-07 22:13:45.795031 GCDTest[68539:2627680] 3------<NSThread: 0x100203c10>{number = 2, name = (null)}
2016-11-07 22:13:45.795051 GCDTest[68539:2627680] 3------<NSThread: 0x100203c10>{number = 2, name = (null)}
6. 异步执行 + 主队列
// 异步 + 主队列
- (void)asyncMain
{
NSLog(@"begin.......");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"2------%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"3------%@",[NSThread currentThread]);
}
});
NSLog(@"end.......");
}
2016-11-07 22:31:36.138 GCDTest[69907:2692640] begin.......
2016-11-07 22:31:36.138 GCDTest[69907:2692640] end.......
2016-11-07 22:31:36.258 GCDTest[69907:2692640] 1------<NSThread: 0x60000007f380>{number = 1, name = main}
2016-11-07 22:31:36.258 GCDTest[69907:2692640] 1------<NSThread: 0x60000007f380>{number = 1, name = main}
2016-11-07 22:31:36.259 GCDTest[69907:2692640] 2------<NSThread: 0x60000007f380>{number = 1, name = main}
2016-11-07 22:31:36.260 GCDTest[69907:2692640] 2------<NSThread: 0x60000007f380>{number = 1, name = main}
2016-11-07 22:31:36.261 GCDTest[69907:2692640] 3------<NSThread: 0x60000007f380>{number = 1, name = main}
2016-11-07 22:31:36.261 GCDTest[69907:2692640] 3------<NSThread: 0x60000007f380>{number = 1, name = main}
4.GCD线程之间的通讯
在iOS开发过程中,我们通常把一些耗时的操作放在其他线程,比如说图片下载、文件上传等耗时操作。而当我们有时候在其他线程完成了耗时操作时,需要回到主线程,那么就用到了线程之间的通讯。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i < 2; ++i) {
NSLog(@"1------%@",[NSThread currentThread]);
}
// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"2-------%@",[NSThread currentThread]);
});
});
运行结果:
2016-11-07 22:58:40.252 GCDTest[71673:2790911] 1------<NSThread: 0x60000026afc0>{number = 3, name = (null)}
2016-11-07 22:58:40.252 GCDTest[71673:2790911] 1------<NSThread: 0x60000026afc0>{number = 3, name = (null)}
2016-11-07 22:58:40.280 GCDTest[71673:2790802] 2-------<NSThread: 0x60000007dec0>{number = 1, name = main}
5.GCD的队列组dispatch_group
有时候我们会有这样的需求:分别异步执行2个耗时操作,然后当2个耗时操作都执行完毕后再回到主线程执行操作。这时候我们可以用到GCD的队列组。
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步操作都执行完毕后,回到主线程...
});
6.GCD的栅栏方法 dispatch_barrier_async
我们有时需要异步执行两组操作,而且第一组操作执行完之后才能开始执行第二组操作。这样我们就需要一个相当于栅栏一样的方法将两组异步执行的操作分隔。
- (void)barrier
{
dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"----1-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"----2-----%@", [NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"----barrier-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"----3-----%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"----4-----%@", [NSThread currentThread]);
});
}
输出结果:
2016-11-07 23:39:03.902 GCDTest[74313:2937448] ----2-----<NSThread: 0x608000078540>{number = 4, name = (null)}
2016-11-07 23:39:03.902 GCDTest[74313:2937439] ----1-----<NSThread: 0x608000078500>{number = 3, name = (null)}
2016-11-07 23:39:03.902 GCDTest[74313:2937439] ----barrier-----<NSThread: 0x608000078500>{number = 3, name = (null)}
2016-11-07 23:39:03.902 GCDTest[74313:2937439] ----3-----<NSThread: 0x608000078500>{number = 3, name = (null)}
2016-11-07 23:39:03.902 GCDTest[74313:2937448] ----4-----<NSThread: 0x608000078540>{number = 4, name = (null)}
如果没有dispatch_barrier_async这个方法,这4个任务执行是没有先后顺序的,完全随机,那么加上这个栅栏方法之后,会严格按照先随机顺序执行完栅栏之前的所有任务,然后再执行栅栏方法之后的任务。
7.GCD的其它方法
-
GCD的延时执行方法
dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 2秒后回到主线程执行这里的代码
NSLog(@"111");
});
NSLog(@"222");
这里的after是会异步延时2秒的,2秒后回到主线程执行代码块的内容,所以输出结果是,先输出222,2秒后再输出111。
-
GCD只执行一次代码
dispatch_once
我们在创建单例、或者有整个程序运行过程中只执行一次的代码时,我们就用到了GCD的dispatch_once
方法。使用dispatch_once
函数能保证某段代码在程序运行过程中只被执行1次。
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 只执行1次的代码(这里面默认是线程安全的)
});