Dispatch Group

本文详细介绍了如何利用Grand Central Dispatch (GCD) 和 DispatchGroup进行任务管理和同步控制。通过实例演示了串行队列与并发队列的任务调度,以及如何使用DispatchGroup来跟踪一组任务的完成状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//在追加到Dispatch Queue中的多个处理全部结束后想执行结束处理时:
//a.只使用一个Serial Dispatch Queue时,只要将想执行的处理全部追加到该 Serial Dispatch Queue中并在最后追加结束处理,即可实现。
//b.使用Concurrent Dispatch Queue时或同时使用多个Dispatch Queue时,可通过Dispatch Group实现。
//无论向什么样的Dispatch Queue中追加处理,使用Dispatch Group都可监视这些处理执行的结果。一旦检测到所有处理执行结束,就可将结束的处理追加到Dispatch Queue中。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//通过dispatch_group_create函数生成Dispatch Group,注意,生成的Dispatch Group在使用结束后必须通过dispatch_release释放(现在GCD对象已经纳入了ARC的管理范围,在纳入之前是需要程序员按照内存管理的思考方式来手动管理GCD对象的),同Dispatch Queue。
dispatch_group_t group = dispatch_group_create();
//通过dispatch_group_async函数追加block到指定的Dispatch Queue中,第一个参数为监视的Dispatch Group。
//注:与追加Block到Dispatch Queue时同样,Block通过dispatch_retain函数持有Dispatch Group,如果Block执行结束,该Block就通过dispatch_release函数释放持有的Dispatch Group。一旦Dispatch Group使用结束,不用考虑属于该Dispatch Group的Block,立即通过dispatch_release函数释放即可。
dispatch_group_async(group, queue, ^{
NSLog(@"blk0");
});
dispatch_group_async(group, queue, ^{
NSLog(@"blk1");
});
dispatch_group_async(group, queue, ^{
NSLog(@"blk2");
});
//通过dispatch_group_notify函数,在追加到Dispatch Queue中(即被Dispatch Group监测)的处理全部执行结束时,会将执行结束处理的Block追加到Dispatch Queue中
//参数1:指定为要监视的Dispatch Group。
//参数2:指定要追加到的Dispatch Queue。
//参数3:结束处理的block
dispatch_group_notify(group, queue, ^{
NSLog(@"结束处理");
});
//也可以使用dispatch_group_wait函数仅等待全部处理执行结束
//等待:意味着一旦调用dispatch_group_wait函数,该函数就处于调用的状态而不返回。执行dispatch_group_wait函数的现在的线程(当前线程)停止。在经过dispatch_group_wait函数中指定的时间或属于指定Dispatch Group的处理全部执行结束之前,执行该函数的线程停止。
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
//参数1:指定Dispatch Group
//参数2:指定最多等待的时间(超时),dispatch_time_t类型值
//返回值:若等待的全部处理执行结束,返回0。否则,返回不为0。
//当参数2为指定等待1秒时:
dispatch_time_t time2 = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);
long result = dispatch_group_wait(group, time2);
if (result == 0) {
//若全部处理执行结束,返回值为0
}else{
//若超时,即还有处理在执行,则返回值不为0
}
//当参数2为DISPATCH_TIME_FORVER,意味着永久等待。只要属于Dispatch Group的处理尚未执行结束,就会一直等待,中途不能取消。因此dispatch_group_wait函数返回时,由于属于Dispatch Group的处理必定全部执行结束,因此返回值为0。
//当参数2为DISPATCH_TIME_NOW,则不用任何等待即可判定属于Dispatch Group的处理是否执行结束

Swift DispatchGroup is a class in the Swift programming language that allows you to synchronize the execution of multiple tasks running on different threads. It helps you to manage the timing of asynchronous operations and avoid race conditions. A DispatchGroup is a lightweight mechanism for tracking a group of tasks. You can add tasks to a group, and the group will notify you when all of the tasks have completed. You can also specify a timeout for the group, after which the group will notify you if any of the tasks have not completed. Here is an example of how to use a DispatchGroup: ``` let group = DispatchGroup() group.enter() // run task 1 group.leave() group.enter() // run task 2 group.leave() group.notify(queue: .main) { // both tasks have completed } ``` In this example, we create a DispatchGroup called `group`. We then use the `enter()` and `leave()` methods to add two tasks to the group. The `notify()` method is called when both tasks have completed, and we can then perform any necessary actions. You can also use a DispatchGroup to wait for a group of tasks to complete before continuing execution. Here's an example: ``` let group = DispatchGroup() group.enter() // run task 1 group.leave() group.enter() // run task 2 group.leave() group.wait() // both tasks have completed ``` In this example, we use the `wait()` method to wait for both tasks to complete before continuing execution. This can be useful if you need to ensure that all tasks have completed before performing additional actions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值