iOS-GCD详解及简单使用
这篇文章主要介绍了iOS-GCD详解的相关资料,并附简单的实例代码,帮助大家学习理解此部分的知识,需要的朋友可以参考下
iOS-GCD介绍
在开发过程中,我们有时会希望把一些操作封装起来延迟一段时间后再执行。iOS开发中,有两种常用的方法可以实现延迟执行,一种是使用GCD,另外一种是使用NSRunLoop类中提供的方法。
前言
对初学者来说,GCD似乎是一道迈不过去的坎,很多人在同步、异步、串行、并行和死锁这几个名词的漩涡中渐渐放弃治疗。本文将使用图文表并茂的方式给大家形象地解释其中的原理和规律。
线程、任务和队列的概念
异步、同步&并行、串行的特点
一条重要的准则
一般来说,我们使用GCD的最大目的是在新的线程中同时执行多个任务,这意味着我们需要两项条件:
能开启新的线程
任务可以同时执行
结合以上两个条件,也就等价“开启新线程的能力+任务同步执行的权利”,只有在满足能力与权利这两个条件的前提下,我们才可以在同时执行多个任务。
所有组合的特点
(一)异步执行+并行队列
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 //异步执行+并行队列
-(void)asyncConcurrent{
//创建一个并行队列
dispatch_queue_tqueue=dispatch_queue_create("标识符",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---");
//使用异步函数封装三个任务
dispatch_async(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
}
(二)异步执行+串行队列
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 //异步执行+串行队列
-(void)asyncSerial{
//创建一个串行队列
dispatch_queue_tqueue=dispatch_queue_create("标识符",DISPATCH_QUEUE_SERIAL);
NSLog(@"---start---");
//使用异步函数封装三个任务
dispatch_async(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
}
(三)同步执行+并行队列
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 //同步执行+并行队列
-(void)syncConcurrent{
//创建一个并行队列
dispatch_queue_tqueue=dispatch_queue_create("标识符",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---");
//使用同步函数封装三个任务
dispatch_sync(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
}
(四)同步执行+串行队列
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 -(void)syncSerial{
//创建一个串行队列
dispatch_queue_tqueue=dispatch_queue_create("标识符",DISPATCH_QUEUE_SERIAL);
NSLog(@"---start---");
//使用异步函数封装三个任务
dispatch_sync(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 -(void)asyncMain{
//获取主队列
dispatch_queue_tqueue=dispatch_get_main_queue();
NSLog(@"---start---");
//使用异步函数封装三个任务
dispatch_async(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
} 打印结果:
---start---
---end---
任务1---{number=1,name=main}
任务2---{number=1,name=main}
任务3---{number=1,name=main}
解释
异步执行意味着
可以开启新的线程
任务可以先绕过不执行,回头再来执行
主队列跟串行队列的区别
队列中的任务一样要按顺序执行
主队列中的任务必须在主线程中执行,不允许在子线程中执行
以上条件组合后得出结果:
所有任务都可以先跳过,之后再来“按顺序”执行
步骤图
(六)同步执行+主队列(死锁)
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 -(void)syncMain{
//获取主队列
dispatch_queue_tqueue=dispatch_get_main_queue();
NSLog(@"---start---");
//使用同步函数封装三个任务
dispatch_sync(queue,^{
NSLog(@"任务1---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务2---%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"任务3---%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
} 打印结果:
---start---
解释
主队列中的任务必须按顺序挨个执行
任务1要等主线程有空的时候(即主队列中的所有任务执行完)才能执行
主线程要执行完“打印end”的任务后才有空
“任务1”和“打印end”两个任务互相等待,造成死锁
步骤图
|
|