步骤图

(六)同步执行+主队列(死锁)
实现代码:
- (void)syncMain{
//获取主队列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"---start---");
//使用同步函数封装三个任务
dispatch_sync(queue, ^{
NSLog(@"任务1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务3---%@", [NSThread currentThread]);
});
NSLog(@"---end---");
}
打印结果:
| 1 |
---start--- |
解释
主队列中的任务必须按顺序挨个执行
任务1要等主线程有空的时候(即主队列中的所有任务执行完)才能执行
主线程要执行完“打印end”的任务后才有空
“任务1”和“打印end”两个任务互相等待,造成死锁
步骤图

写在结尾的话
以上就是我对GCD的基础知识和几种组合的理解。
注:相关教程知识阅读请移步到IOS开发频道。










