dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 2秒后异步追加任务代码到主队列 NSLog(@"----执行任务---当前线程%@",[NSThread currentThread]); });复制代码
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"----只执行一次的任务---当前线程%@",[NSThread currentThread]); });复制代码
//并发队列 dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //串行队列 dispatch_queue_t queue = dispatch_queue_create("com.test.testQueue", DISPATCH_QUEUE_SERIAL); //你可以试试并发和串行的区别 dispatch_apply(10, globalQueue, ^(size_t index) { NSLog(@"执行第%zd次的任务,%@",index,[NSThread currentThread]); [NSThread sleepForTimeInterval:1]; });复制代码
//创建队列组 dispatch_group_t group = dispatch_group_create(); //全局串行队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_enter(group); dispatch_async(queue, ^{ // 第一个任务 [NSThread sleepForTimeInterval:2]; NSLog(@"执行第一个任务"); dispatch_group_leave(group); }); dispatch_group_enter(group); dispatch_async(queue, ^{ // 第二个任务 [NSThread sleepForTimeInterval:2]; NSLog(@"执行第二个任务"); dispatch_group_leave(group); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"执行最后的汇总任务"); }); 复制代码
//创建信号量为2 dispatch_semaphore_t semaphore = dispatch_semaphore_create(2); //全局并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //任务1 dispatch_async(queue, ^{ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"----开始执行第一个任务---"); [NSThread sleepForTimeInterval:2]; NSLog(@"----结束执行第一个任务---",); dispatch_semaphore_signal(semaphore); }); //任务2 dispatch_async(queue, ^{ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"----开始执行第二个任务---"); [NSThread sleepForTimeInterval:1]; NSLog(@"----结束执行第二个任务---"); dispatch_semaphore_signal(semaphore); }); //任务3 dispatch_async(queue, ^{ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"----开始执行第三个任务---",); [NSThread sleepForTimeInterval:2]; NSLog(@"----结束执行第三个任务---"); dispatch_semaphore_signal(semaphore); });复制代码
//全局并发队列 dispatch_queue_t queue = dispatch_queue_create("com.test.testQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"栅栏前的任务"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"栅栏前的任务"); }); //栅栏 dispatch_barrier_async(queue, ^{ // 等待处理 [NSThread sleepForTimeInterval:2]; NSLog(@"-栅栏等待-"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"栅栏后的任务"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"栅栏后的任务"); });复制代码
//串行队列 dispatch_queue_t queue = dispatch_queue_create("com.test.testQueue", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ // 执行第一个任务 NSLog(@"----执行第一个任务---"); }); dispatch_async(queue, ^{ // 执行第二个任务 NSLog(@"开始执行第二个任务"); [NSThread sleepForTimeInterval:5]; NSLog(@"结束执行第二个任务"); }); dispatch_async(queue, ^{ // 执行第三个任务 NSLog(@"开始执行第三个任务"); [NSThread sleepForTimeInterval:5]; NSLog(@"结束执行第三个任务"); }); //此时发现意外情况,挂起队列 NSLog(@"suspend"); dispatch_suspend(queue); //挂起3秒之后,恢复正常 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //恢复队列 NSLog(@"3秒后恢复resume"); dispatch_resume(queue); });复制代码
定时器 GCD的定时器比NSTimer有更高的进度,而且避免了NSTimer的三大缺陷(RunLoop,不能跨越线程操作,内存泄漏风险)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建定时器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//构建参数。设置从何时开始/设置时间间隔
dispatch_source_set_timer(self.timer, dispatch_time(DISPATCH_TIME_NOW, 0* NSEC_PER_SEC), 1.0 * NSEC_PER_SEC, 0);
//要执行的时间
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"多久打印一次");
});
//运行GSD
dispatch_resume(self.timer);
//取消定时器
//dispatch_cancel(self.timer); self.timer = nil;复制代码
本节我们主要简单的梳理了GCD的一些实用API,可以发现这些API是纯c语言的,并且相关API繁多,在实际编码过程中即使有代码提示,往往编码效率还是比较慢的,造成一定的使用门槛。当然,你也通过自定义代码块来提升效率。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。