美文网首页
iOS-GCD学习总结

iOS-GCD学习总结

作者: SuperDawn_0828 | 来源:发表于2017-07-31 11:25 被阅读24次

    1、前言

    GCD的学习中一定要理解任务,同步、异步,串行、并发的概念,以及他们之间相互结合运行的效果。

    2、GCD的基本概念

    • 任务:任务就是将要在线程中执行的代码,用block快封装好,添加到指定的执行方式,等待CPU从队列中取出任务放到对应的线程中执行。
    • 同步:任务一个接着一个的执行,前一个执行完,后一个才开始执行;不开启新的线程。
    • 异步:任务同一时间可以一起执行;开启多个新的线程。
    • 队列:装在线程任务的队形结构,有串行队列和并发队列两种队列。
    • 并发队列:线程可以同一时间一起执行。
    • 串行队列:线程只能依次有序的执行。

    3、队列的创建方法

    队列的创建方法是使用:

    dispatch_queue_create(const char *_Nullable label, dispatch_queue_attr_t _Nullable attr)
    

    函数创建一个队列对象。
    第一个参数表示队列的唯一标识符,第二个参数用于指定队列是串行队列(DISPATCH_QUEUE_SERIAL) 还是并发队列(DISPATCH_QUEUE_CONCURRENT)。

    串行队列的创建方法:

    dispatch_queue_t queue = dispatch_queue_create("com.gcd.serail", DISPATCH_QUEUE_SERIAL);
    

    并发队列的创建方法:

    dispatch_queue_t queue = dispatch_queue_create("com.gcd.concurrent", DISPATCH_QUEUE_CONCURRENT);
    

    其它两个队列:

    • 主队列:主队列负责在主线程上调度任务,如果主线程上已经有任务在执行,主队列等待主线程上任务执行完后在调度任务执行。
      主队列获取方法:

      dispatch_queue_t mainQueue = dispatch_get_main_queue();
      
    • 全局并发队列:全局并发队列是系统为了方便使用对线程,提供的一个并发队列。
      全局并发队列的获取方法:

      dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
      

      全局并发队列获取方法第一个参数表示队列的优先级,第二个参数是扩展位,可以写入任意值,没有实际意义。

    4、同步、异步任务的创建方法

    同步任务:
    同步任务创建函数如下:

    dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block)
    

    第一个参数为任务需要添加到的队列,第二个参数为需要执行的任务。
    同步任务不会开启新的线程。

    异步任务:
    异步任务创建函数如下:

     dispatch_async(dispatch_queue_t queue, dispatch_block_t block)
    

    第一个参数为任务需要添加到的队列,第二个参数为需要执行的任务。
    异步任务会开启新的线程。

    5、GCD的使用方法

    GCD的使用由多种队列(主队列、并发队列、串行队列)和两种执行方式(同步、异步)进行结合。因此有多种组合方式:

    并发异步:
    并发异步开启多个线程,多任务交替执行。

    dispatch_queue_t queue = dispatch_queue_create("com.gcddemo.asyncconcurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    输出结果:

    2017-07-29 18:34:52.401 GCDDemo[22711:165644] asyncIndex: 1-0, currentThread: <NSThread: 0x60800007f400>{number = 3, name = (null)}
    2017-07-29 18:34:52.401 GCDDemo[22711:165630] asyncIndex: 2-0, currentThread: <NSThread: 0x6000000754c0>{number = 4, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165644] asyncIndex: 1-1, currentThread: <NSThread: 0x60800007f400>{number = 3, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165630] asyncIndex: 2-1, currentThread: <NSThread: 0x6000000754c0>{number = 4, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165644] asyncIndex: 1-2, currentThread: <NSThread: 0x60800007f400>{number = 3, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165630] asyncIndex: 2-2, currentThread: <NSThread: 0x6000000754c0>{number = 4, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165644] asyncIndex: 1-3, currentThread: <NSThread: 0x60800007f400>{number = 3, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165630] asyncIndex: 2-3, currentThread: <NSThread: 0x6000000754c0>{number = 4, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165644] asyncIndex: 1-4, currentThread: <NSThread: 0x60800007f400>{number = 3, name = (null)}
    2017-07-29 18:34:52.402 GCDDemo[22711:165630] asyncIndex: 2-4, currentThread: <NSThread: 0x6000000754c0>{number = 4, name = (null)}
    

    从输出结果上可以看出上述例子创建了两个分线程,交替的执行任务。

    并发同步:
    任务按顺序执行,不开启新的线程。

    dispatch_queue_t queue = dispatch_queue_create("com.gcddemo.syncconcurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
            for (int index = 0; index < 5; index ++) {
            NSLog(@"syncIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"syncIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    输出结果:

    2017-07-29 18:36:26.243 GCDDemo[22711:165589] syncIndex: 1-0, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.243 GCDDemo[22711:165589] syncIndex: 1-1, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.244 GCDDemo[22711:165589] syncIndex: 1-2, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.244 GCDDemo[22711:165589] syncIndex: 1-3, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.244 GCDDemo[22711:165589] syncIndex: 1-4, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.244 GCDDemo[22711:165589] syncIndex: 2-0, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.245 GCDDemo[22711:165589] syncIndex: 2-1, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.245 GCDDemo[22711:165589] syncIndex: 2-2, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.245 GCDDemo[22711:165589] syncIndex: 2-3, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    2017-07-29 18:36:26.245 GCDDemo[22711:165589] syncIndex: 2-4, currentThread: <NSThread: 0x600000072940>{number = 1, name = main}
    

    输出结果上可以看到当前的线程为主线程,并没有开启新的线程,任务按先后顺序执行。

    串行异步:
    只开启一条分线程,任务按照循序执行。

    dispatch_queue_t queue = dispatch_queue_create("com.gdcdemo.asyncserail", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncserialIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncserialIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    输出结果:

    2017-07-29 18:41:29.923 GCDDemo[23359:171236] asyncserialIndex: 1-0, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.923 GCDDemo[23359:171236] asyncserialIndex: 1-1, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.923 GCDDemo[23359:171236] asyncserialIndex: 1-2, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 1-3, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 1-4, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 2-0, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 2-1, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 2-2, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.924 GCDDemo[23359:171236] asyncserialIndex: 2-3, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    2017-07-29 18:41:29.925 GCDDemo[23359:171236] asyncserialIndex: 2-4, currentThread: <NSThread: 0x60000007cd00>{number = 3, name = (null)}
    

    从输出结果上可以看出,上述例子值创建了一条分线程,任务按照先后循序执行。

    串行同步:
    任务按先后循序执行,不开启新的线程。

    dispatch_queue_t queue = dispatch_queue_create("com.gcddemo.syncserail", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"syncserialIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"syncserialIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    输出结果:

    2017-07-29 18:47:04.147 GCDDemo[23910:176279] syncserialIndex: 1-0, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.147 GCDDemo[23910:176279] syncserialIndex: 1-1, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.147 GCDDemo[23910:176279] syncserialIndex: 1-2, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.147 GCDDemo[23910:176279] syncserialIndex: 1-3, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 1-4, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 2-0, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 2-1, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 2-2, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 2-3, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    2017-07-29 18:47:04.148 GCDDemo[23910:176279] syncserialIndex: 2-4, currentThread: <NSThread: 0x60000006c400>{number = 1, name = main}
    

    从输出结果上可以看出任务按照先后循序在主线程上执行,没有开启新的线程。

    主队列异步:
    任务按先后顺序在主线程中执行。

    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncMianIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int index = 0; index < 5; index ++) {
            NSLog(@"asyncMianIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    输出结果:

    2017-07-31 09:48:54.844 GCDDemo[7385:64936] asyncMianIndex: 1-0, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.844 GCDDemo[7385:64936] asyncMianIndex: 1-1, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.844 GCDDemo[7385:64936] asyncMianIndex: 1-2, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.844 GCDDemo[7385:64936] asyncMianIndex: 1-3, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.844 GCDDemo[7385:64936] asyncMianIndex: 1-4, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.845 GCDDemo[7385:64936] asyncMianIndex: 2-0, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.845 GCDDemo[7385:64936] asyncMianIndex: 2-1, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.845 GCDDemo[7385:64936] asyncMianIndex: 2-2, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.845 GCDDemo[7385:64936] asyncMianIndex: 2-3, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    2017-07-31 09:48:54.845 GCDDemo[7385:64936] asyncMianIndex: 2-4, currentThread: <NSThread: 0x608000261580>{number = 1, name = main}
    

    从输出结果可以去看出,任务按照先后循序,在主线程上执行。

    主队列同步:
    在主队列执行同步任务时,由于任务之间相互等待,会造成崩溃。

    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        for (int index = 0; index <= 10; index ++) {
            NSLog(@"syncMainIndex: 1-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int index = 0; index <= 10; index ++) {
            NSLog(@"syncMainIndex: 2-%d, currentThread: %@", index, [NSThread currentThread]);
        }
    });
    

    代码执行的顺序不是从上到下,而是先执行外部的代码,发现有两个同步任务加载到主队列,立即去执行两个同步任务,由于任务执行需要等到另外一个任务执行完才可以,两个任务就相互等待,最终造成崩溃。

    上述总结:

    串行 并发 主队列
    同步 不开启新的线程 不开启新的线程 线程崩溃
    异步 开启一条新的线程 开启多条新的线程 不开启新的线程,在主线程执行任务

    6、其他GCD函数

    延时函数:
    GCD延时创建方法:

    dispatch_after(dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block)
    

    第一个参数是延时的时间,第二参数是加载任务的队列,第三个参数是延时结束后执行的任务。

    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));
    dispatch_after(time, queue, ^{
        NSLog(@"after 3s, currentThread: %@", [NSThread currentThread]);
    });
    

    上述列子中创建了一个延时时间从当前时间开始延时3秒,将任务加载到最队列中去,当延时结束在主线程中执行任务。

    栅栏:
    队列加载栅栏函数携带的任务时,队列中的其他任务要等待栅栏函数中的任务执行结束才可以执行。
    通过该功能可以实现不同线程任务执行的先后顺序以及实现锁的功能。
    栅栏函数不要放到主队列以及全局并发队列中。运用栅栏函数一定要预防卡线程。

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        NSLog(@"berrier index 1, currentThread: %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"berrier index 2, currentThread: %@", [NSThread currentThread]);
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"current thread: %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"berrier index 3, currentThread: %@", [NSThread currentThread]);
    });
    

    上述例子输出结果:

    2017-07-31 10:49:44.534 GCDDemo[12353:106669] berrier index 2, currentThread: <NSThread: 0x608000076bc0>{number = 7, name = (null)}
    2017-07-31 10:49:44.534 GCDDemo[12353:113335] berrier index 1, currentThread: <NSThread: 0x60000006ba40>{number = 6, name = (null)}
    2017-07-31 10:49:44.534 GCDDemo[12353:113336] current thread: <NSThread: 0x60000006edc0>{number = 8, name = (null)}
    2017-07-31 10:49:44.534 GCDDemo[12353:113337] berrier index 3, currentThread: <NSThread: 0x600000078180>{number = 9, name = (null)}
    

    输出结果上可以看到开启了多条新的线程,将任务分成两组,第一组执行结束后才执行第二组任务。

    once函数:
    dispatch_once多用于单例的创建,下属代码执行一次后,再次执行不会有log打印输出。

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
          NSLog(@"once, current thread: %@", [NSThread currentThread]);
    });
    

    队列组:
    队列组的创建方法如下:

    dispatch_group_t group = dispatch_group_create()
    

    队列组是将加载任务的队列放到队列组中,当队列中的任务执行完后,执行如下方法:

    dispatch_group_notify(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)
    

    如合成网络图片及其他需要在分线程中工作,等待个线程完成工作后才执行的任务。

    __block int a = 0, b = 0, c = 0;
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"dispath-group index: 1, current thread: %@", [NSThread currentThread]);
        a = 1;
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"dispath-group index: 2, current thread: %@", [NSThread currentThread]);
        b = 3;
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"dispath-group index: 3, current thread: %@", [NSThread currentThread]);
        c = 5;
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"group-notify current thread: %@", [NSThread currentThread]);
        NSLog(@"a + b + c = %d", a + b + c);
    });
    

    上述例子输出结果如下:

    2017-07-31 10:41:58.992 GCDDemo[12353:106684] dispath-group index: 1, current thread: <NSThread: 0x608000075ec0>{number = 3, name = (null)}
    2017-07-31 10:41:58.992 GCDDemo[12353:106671] dispath-group index: 2, current thread: <NSThread: 0x608000075d80>{number = 4, name = (null)}
    2017-07-31 10:41:58.992 GCDDemo[12353:106668] dispath-group index: 3, current thread: <NSThread: 0x608000075c00>{number = 5, name = (null)}
    2017-07-31 10:41:58.993 GCDDemo[12353:106620] group-notify current thread: <NSThread: 0x600000066bc0>{number = 1, name = main}
    2017-07-31 10:41:58.993 GCDDemo[12353:106620] a + b + c = 9
    

    从输出结果上可以看出对a、b、c的赋值分别是在不同的线程中完成的,各个线程赋值结束后在回到主线程对a、b、c进行加法运算。

    快速迭代:
    快速迭代可以同时遍历多个数字,多用于循环量特别大的操作中。如加载相册图片,当图片有上百张的时候可能会造成卡顿现象,就可以使用快速迭代的方法。

    dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
        NSLog(@"apply index: %zu, current thread: %@", index, [NSThread currentThread]);
    });
    

    输出结果如下:

    2017-07-31 11:14:51.059 GCDDemo[12353:106620] apply index: 0, current thread: <NSThread: 0x600000066bc0>{number = 1, name = main}
    2017-07-31 11:14:51.059 GCDDemo[12353:106669] apply index: 1, current thread: <NSThread: 0x608000076bc0>{number = 7, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:131416] apply index: 2, current thread: <NSThread: 0x608000077240>{number = 13, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:131417] apply index: 3, current thread: <NSThread: 0x608000077540>{number = 14, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:106620] apply index: 4, current thread: <NSThread: 0x600000066bc0>{number = 1, name = main}
    2017-07-31 11:14:51.059 GCDDemo[12353:131416] apply index: 6, current thread: <NSThread: 0x608000077240>{number = 13, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:106669] apply index: 5, current thread: <NSThread: 0x608000076bc0>{number = 7, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:131417] apply index: 7, current thread: <NSThread: 0x608000077540>{number = 14, name = (null)}
    2017-07-31 11:14:51.059 GCDDemo[12353:106620] apply index: 8, current thread: <NSThread: 0x600000066bc0>{number = 1, name = main}
    2017-07-31 11:14:51.059 GCDDemo[12353:131416] apply index: 9, current thread: <NSThread: 0x608000077240>{number = 13, name = (null)}
    

    从输出结果上可以看出该函数开启了多条新的线程,进行数字的遍历操作。

    7、总结

    GCD的使用过程是指定任务的执行方法(同步、异步),并把任务添加到串行队列或者并发队列中,通过CPU调度执行。
    上述只是GCD的基本使用方法,具体要结合使用环境,灵活运用。

    相关文章

      网友评论

          本文标题:iOS-GCD学习总结

          本文链接:https://www.haomeiwen.com/subject/xkdclxtx.html