前言:

前天学了IOS的NSOperation基本使用,我们得知NSOperation也是基于IOS GCD(Grand Central Dispatch)实现,其实在做IOS开发中GCD已经基本上能够满足大部分需求。作为IOS开发工程师很有必要对GCD做个全面了解,今天一边写demo一边对比总结一下GCD使用。

了解GCD

GCD是Grand Central Dispatch的简称,它是基于C语言的。如果使用GCD,完全由系统管理线程,我们不需要编写线程代码。只需定义想要执行的任务,然后添加到适当的调度队列(dispatch queue)。GCD会负责创建线程和调度你的任务,系统直接提供线程管理。

执行方式

使用线程就离不开线程队列的执行方式和任务的执行方式,大致分以下几个:

  • 串行(Serial):让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)

  • 并行(Concurrent):可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务)并发功能只有在异步(dispatch_async)函数下才有效。

  • 同步(Synchronous):在当前线程中执行任务,不具备开启新线程的能力

  • 异步(Asynchronous):在新的线程中执行任务,具备开启新线程的能力

调度队列

上面了解到Dispatch 通过分发开发者提供的不同queue来调度任务,我们来看下GCD有哪些队列。

队列类型 创建方式

主线程串行队列(mian)

dispatch_get_main_queue();

自定义串行队列(Serial)

dispatch_queue_create("MySerialQueue", DISPATCH_QUEUE_SERIAL);

自定义并行队列(Concurrent)

dispatch_queue_create("MyConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);

全局并行队列(global)

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

添加执行任务的方式大致有两种:dispatch_sync 同步执行任务函数,不会开启新的线程,dispatch_async 异步执行任务函数,会开启新的线程,接下来分别看下他们的使用示例;

主线程串行队列(mian)

    dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(mainQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(mainQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

注意:

主线程串行队列执行在主线程中,一般使用都是调用dispatch_async添加任务,使用dispatch_sync添加任务会导致死锁问题。

运行结果:

通过运行结果可以得知,虽然任务是通过dispatch_async添加执行的,但是并没有创建子线程去执行任务,而是执行在主线程中。

自定义串行队列(Serial)

有时我们需要创建一个串行任务并且需要执行在子线程中,这时就需要创建串行队列,进行异步添加调用方式执行任务。

    dispatch_queue_t mySerialQueue = dispatch_queue_create("MySerialQueue", DISPATCH_QUEUE_SERIAL);

    dispatch_async(mySerialQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(mySerialQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(mySerialQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

运行结果:

可以看出串行队列异步执行创建了一个线程,并且是依次执行。

全局并行队列(global)

相对与串行任务队列,有时我们需要同时执行多个任务,这个时候我们就需要使用并行任务队列了,这里我们采用全部并行队列。

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_async(globalQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(globalQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(globalQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

这里可以根据不同的优先级创建不同的全部任务队列

  • DISPATCH_QUEUE_PRIORITY_HIGH //高优先级
  • DISPATCH_QUEUE_PRIORITY_DEFAULT //默认优先级
  • DISPATCH_QUEUE_PRIORITY_LOW  //低优先级
  • DISPATCH_QUEUE_PRIORITY_BACKGROUND //后台执行

运行结果:

三个任务几乎是同时进行的,而且动态为每个任务开辟了一个线程用于执行任务。并行队列尽量使用异步添加任务的方式调用,同步添加任务方式调用不会创建子线程而是任务全部同时执行在主线程中导致UI卡死。

自定义并行队列(Concurrent)

    dispatch_queue_t myConcurrentQueue = dispatch_queue_create("MyConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(myConcurrentQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_async(myConcurrentQueue, ^{
//在block里写要执行的任务(代码)
NSLog(@"currentThread3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

运行结果和全部并行队列类似。

调度队列组

有时无论我们串行执行多任务还是并行执行多任务,需要这个任务组全部执行完毕之后通知我们,这里就需要用到队列组了。

    dispatch_group_t group = dispatch_group_create();
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("MyConcurrentQueue", DISPATCH_QUEUE_CONCURRENT); //dispatch_group_async用于把不同的任务归为一组
//dispatch_group_notify当指定组的任务执行完毕之后,执行给定的任务 dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"currentThread-1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"currentThread-2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"currentThread-3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
}); dispatch_group_notify(group, myConcurrentQueue, ^{
NSLog(@"currentThread-g:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

运行结果:

GCD数据同步问题

既然是多任务执行,就难以避免会出现多任务同时访问同一数据的问题,就会遇到同步的问题,串行队列不太会遇见数据同步的问题,但是并行队列一定会有数据同步的问题,IOS GCD考虑的很全面通过调用dispatch_barrier_async函数添加任务来隔离其他的任务,起到一个栅栏的作用,它等待所有位于barrier函数之前的操作执行完毕后执行,并且在barrier函数执行之后,barrier函数之后的操作才会得到执行,该函数需要同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用。

    dispatch_queue_t conCurrentQueue = dispatch_queue_create("myConCurrentQueue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(conCurrentQueue, ^{
NSLog(@"currentThread-1:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"currentThread-2:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});
dispatch_barrier_async(conCurrentQueue, ^{
[NSThread sleepForTimeInterval:1.0];
NSLog(@"currentThread-b:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
[NSThread sleepForTimeInterval:1.0];
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"currentThread-3:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"currentThread-4:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

运行结果:

GCD其他常用函数

dispatch_once保证在app运行期间,block中的代码只执行一次

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只执行一次");
//这个block里的代码,在程序执行过程中只会执行一次。
//比如在这里些单例的初始化
// static YourClass *instance = nil;
// instance = [[YourClass alloc] init];
});

dispatch_after延时添加到队列

    double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//dispatch_after函数是延迟执行某个任务,任务既可以在mainQueue中进行也可以在其他queue中进行.既可以在serial队列里执行也可以在concurrent队列里执行。
NSLog(@"currentThread:%@ isMainThread:%@",[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

dispatch_apply把一项任务提交到队列中多次执行,具体是并行执行还是串行执行由队列本身决定

NSArray *array = [NSArray arrayWithObjects:@"who",@"is",@"lcj", nil];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_apply([array count], queue, ^(size_t index) {
NSLog(@"%@ : currentThread:%@ isMainThread:%@",[array objectAtIndex:index],[NSThread currentThread],[[NSThread currentThread] isMainThread]?@"YES":@"NO");
});

总结:

用了GCD之后更见佩服Apple公司了,封装的sdk 真是太完善了。简单的总结了如何使用GCD,希望在以后的使用中有个大致了解。

IOS任务管理之GCD使用的更多相关文章

  1. [iOS]深入理解GCD

    看到一篇很好的文章,本来想翻译的,但发现已经有人翻译了,遂简单整理了一下,方便阅读学习 新博客[wossoneri.com] 什么是GCD GCD(Grand Central Dispatch)是li ...

  2. iOS 开发之 GCD 不同场景使用

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  3. iOS 开发之 GCD 基础

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  4. 玩转iOS开发:iOS中的GCD开发(三)

    上一章, 我们了解到了GCD里的一些队列和任务的知识, 也实践了一下, 同时我们也对主队列的一些小情况了解了一下, 比如上一章讲到的卡线程的问题, 如果没有看的朋友可以去看看玩转iOS开发:iOS中的 ...

  5. iOS开发之GCD

    GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制.它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务. GCD中的FIFO队列称为 ...

  6. ios线程和GCD

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

  7. IOS并发编程GCD

    iOS有三种多线程编程的技术 (一)NSThread  (二)Cocoa NSOperation (三)GCD(全称:Grand Central Dispatch) 这三种编程方式从上到下,抽象度层次 ...

  8. IOS多线程(GCD)

    简介 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首次 ...

  9. ios开发多线程--GCD

    引言 虽然GCD使用很广,而且在面试时也经常问与GCD相关的问题,但是我相信深入理解关于GCD知识的人肯定不多,大部分都是人云亦云,只是使用过GCD完成一些很简单的功能.当然,使用GCD完成一些简单的 ...

随机推荐

  1. Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(三)

    继续, 上文提到的是用BUILD_STATIC_JAVA_LIBRARY在Android4.2源码下编译出来的jar包可以在Eclipse(SDK版本4.1)上使用, 找来Android6.0的源码, ...

  2. js中,怎么解决cookie里面中文乱码问题

    呵呵,我查了百度和谷歌,都没找到解决方案,但是,最终直接结合两个函数就可以了,哈哈哈,开心ing function getCookie(name) { var prefix = name + &quo ...

  3. PopupWindow的基本使用

    1>写好布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  4. css 之!important

    主要是自己犯了个错误: 把 !important 放到了分号后面; 正确写法写法: .current{ background-color: #f1f1f1; border-left: 2px soli ...

  5. 深度学习caffe:Ubuntu16.04安装指南(1)

    caffe [CPU ONLY] 2017-01-15 最简单的安装配置方式: 不用GPU加速,使用OPENCV2.4图像库, 这是根据官方教程(链接如下)简化而得到. Ubuntu 16.04 or ...

  6. 转载自前端开发:CSS设置滚动条样式

    浏览器默认的滚动条样子太过屌丝了,得自己动手整整.记得IE浏览器有几个设置滚条的样式,不过比较鸡肋,只能设置颜色之类的,而且webkit下面也不支持.无意间看到网易邮箱的滚动条样子很好看,一开始以为是 ...

  7. Ubuntu14.04下搭建VPN服务

    直接上步骤: 1.第一步需要安装PPTP,以用来提供VPN服务. sudo apt-get install pptpd 如果有问题的话比如提示找不到之类的,apt-get update 一下应该就可以 ...

  8. Grunt之watch详解

    Grunt 之 watch 和 livereload 现在 watch 中已经集成了 livereload ,所以把它们放在一起说明. watch 可以监控特定的文件,在添加文件.修改文件.或者删除文 ...

  9. 简述Android系统内存不足时候,内存回收机制

    当Android系统的内存不足时,会根据以下的内存回收规则来回收内存: 1.先回收与其他Activity或Service/Intent Receiver无关的进程(即优先回收独立的Activity) ...

  10. 简述Hibernate三种开发方式

    1.由domain object->mapping->db(官方推荐) 2.由db开始,用工具生成mapping和domain object(使用较多) 3.由映射文件开始