GCD(Grand Central Dispatch)
1. 什么是GCD
GCD实现了异步执行任务。开发者只需将自己定义的任务追加到合适的派发队列(Dispatch Queue)中,GCD就能生成必要的线程执行自定义的任务。
2. 派发队列(Dispatch Queue)
派发队列是执行任务的等待队列,按照追加任务的顺序(先进先出)执行任务。
GCD中的派发队列分为两种,一种是等待正在执行的任务的串行派发队列(Serial Dispatch Queue),另一种是不等待正在执行中的任务的并行派发队列(Concurrent Dispatch Queue)。
串行队列(Serial Dispatch Queue)
- 队列中的任务会等待正在执行的任务执行结束,有序执行(排队执行)
- 一个串行队列只生成并使用一个线程,只能执行向它追加的任务
- 如果生成了多个串行队列,这些串行队列将并发执行
- 在多个线程要更新相同的资源,导致的数据竞争时,使用串行队列
- Dispatch Queue系统中为我们提供的串行队列是Main Dispatch Queue,即在主线程中执行的派发队列。追加到Main Dispatch Queue的任务都是在主线程的RunLoop中执行,例如用户界面的更新操作。
//创建一个串行队列
dispatch_queue_t myQueue = dispatch_queue_create("com.example.gcd", NULL);
// 获得主线程队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
并发队列(Concurrent Dispatch Queue)
- 不用等待正在执行的任务处理结束,可以并发执行多个任务,此时并发执行的任务数量取决于当前系统的状态。
- 并发队列中的任务不能产生数据竞争问题
- Dispatch Queue系统中为我们提供的并发队列Global Dispatch Queue ,是所有应用程序都能使用的并发队列,需要在程序中使用并发队列只需将获得Global Dispatch Queue。
- Global Dispatch Queue有四个优先级,分别为高优先级(High Priority)、默认优先级(Default Priority)、低优先级(Low Priority)和后台优先级(Background Priority)。需要注意的是,在向Global Dispatch Queue中追加任务时,应选择与任务的优先级相同的Global Dispatch Queue。但是用于Global Dispatch Queue的线程并不保证实时性,其执行的优先级只是大致的判断。
//创建一个并行队列
dispatch_queue_t myQueue = dispatch_queue_create("com.example.gcd", DISPATCH_QUEUE_CONCURRENT);
// 获得默认优先级的全局队列
dispatch_queue_t myQueue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
3. GCD中常见API
为了防止我说的不准确,下面列举了一些我认为重要的官方的API
- dispatch_set_target_Queue 变更生成的Dispatch Queue的优先级
void dispatch_set_target_queue(dispatch_object_t object, dispatch_queue_t queue);
- dispatch_after 并不在指定的时间后执行,而是在指定时间后,追加任务到派发队列中去。
void dispatch_after(dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block);
- Dispatch Group
GCD中的特性,能够将队列中的任务分组;
在等待的派发队列中的任务执行完毕后,追加新任务到派发队列中。
此时可以使用: - dispatch_group_notify函数等待分组中的任务执行完毕,再追加新任务到指定队列中。
void dispatch_group_notify(
dispatch_group_t group, // The dispatch group to observe.
dispatch_queue_t queue, // The queue to which the supplied block will be submitted when the group completes.
dispatch_block_t block // The block to submit when the group completes.
);
- dispatch_group_wait函数,等待分组中的任务执行结束,当其返回值为0时,表示分组中的任务执行完毕。
/*
* Returns zero on success (all blocks associated with the group completed
* within the specified timeout) or non-zero on error (i.e. timed out).
*/
long dispatch_group_wait(
dispatch_group_t group,
dispatch_time_t timeout // When to timeout (see dispatch_time). As a convenience, there are the DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
);
- dispatch_barrier_async 栅栏
栅栏必须单独执行,不能与其他任务并发执行,因此,栅栏只对并发队列有意义。
栅栏只有等待当前队列所有并发任务都执行完毕后,才会单独执行,待其执行完毕,再按照正常的方式继续向下执行。
执行过程
(1)等待追加到并发队列上的任务全部执行完
(2)再将dispatch_barrier_async中指定的处理任务追加到该并发队列中
(3)等待dispatch_barrier_async中追加的任务处理完毕后
(4)并发队列恢复正常操作
可以提高访问数据库和文件的效率和安全性
/*
* block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
*/
void dispatch_barrier_async(
dispatch_queue_t queue, // The target dispatch queue to which the block is submitted.The system will hold a reference on the target queue until the block has finished.
dispatch_block_t block // The block to submit to the target dispatch queue. This function performs Block_copy() and Block_release() on behalf of callers.
);
- dispatch_apply 按照指定的次数将指定的块追加到指定的派发队列中,并等待全部任务执行结束
/*
* Each invocation of the block will be passed the current index of iteration.
*/
void dispatch_apply(
size_t iterations, // The number of iterations to perform.
dispatch_queue_t queue, // The target dispatch queue to which the block is submitted.s
void (^block)(size_t) // The block to be invoked the specified number of iterations. The result of passing NULL in this parameter is undefined.
);
-
dispatch_suspend/dispatch_resume 挂起/恢复
对已经执行的任务并没有作用,线程挂起后,派发队列中从第一个未开始执行的任务开始暂停执行,线程恢复后,再由停止的地方重新开始执行。 -
单例的实现
(1)同步块 @synchronization
会出现的问题:所有同步块会彼此抢夺同一把锁
(2)dispatch_once 可以执行只需要运行一次的安全代码,是线程安全,高效的。
/*
* A predicate for use with dispatch_once(). It must be initialized to zero.
* Note: static and global variables default to zero.
*/
typedef long dispatch_once_t;
void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
4. 同步任务与异步任务
- 主要影响:能不能开启新的线程
- 同步:只是在当前线程中执行任务,不具有开启新线程的能力。
dispatch_sync 同步执行,等待任务处理结束
- 异步:可以在新线程中执行任务,具有开启新线程的能力。
dispatch_async 异步执行,不等待任务处理结束
5. 并行队列和串行队列
- 主要影响:任务的执行方式
- 并发:允许多个任务并发(同时)执行。
- 串行:一个任务执行完毕后,再执行下一个任务。
6. 对比
并行队列 | 串行队列 | |
---|---|---|
同步任务 | 不需要创建线程 | 不需要新建线程 |
异步任务 | 有多少个任务,就开N个线程执行 | 需要一个子线程(无需关心线程的创建和回收) |
7. GCD与NSOperationQueue的对比
- NSOperationQueue的底层实现都是用GCD实现的,它是对GCD的进一步的封装。接下来就对比一下GCD与NSOperationQueue的功能:
- GCD的实现是由底层的C语言编写,在队列中执行的任务都是块代码;而NSOperationQueue及相关类提供的对象都是OC的对象,NSOperation的对象提供了更多的操作。
- NSOperationQueue中可以随时取消已经设定即将要执行,但尚未执行的任务(已经开始执行的任务是无法停止执行的)。而在GCD中并不是不能实现同样的功能,而是并不像NSOperationQueue的操作那么简单而已。
- NSOperation可以设置任务间的依赖关系,举个简单的例子:任务QA依赖任务QB,即使这两个任务在同一个队列中,前者等待后者执行完后再执行。
- NSOperation可以应用KVO,监听任务是否完成/取消的状态。
- 可以在NSOperation的对象中,设置任务的优先级,可以使同一个并发队列中的任务区分先后顺序地执行。而GCD中可以区分队列间的先后顺序,要区分队列中任务的优先级需要大量的代码才能实现。
- NSOperation具有继承性
网友评论