iOS 开发学习成长之路知识点详解开发技巧分类

iOS-图文表并茂,手把手教你GCD

2016-10-23  本文已影响1452人  Aaron_ZhangKH

前言

对初学者来说,GCD似乎是一道迈不过去的坎,很多人在同步、异步、串行、并行和死锁这几个名词的漩涡中渐渐放弃治疗。本文将使用图文表并茂的方式给大家形象地解释其中的原理和规律。

线程、任务和队列的概念

异步、同步 & 并行、串行的特点

一条重要的准则

一般来说,我们使用GCD的最大目的是在新的线程同时执行多个任务,这意味着我们需要两项条件:

所有组合的特点

(一)异步执行 + 并行队列

实现代码:
//异步执行 + 并行队列
- (void)asyncConcurrent{
    //创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("标识符", DISPATCH_QUEUE_CONCURRENT);
    
    NSLog(@"---start---");

    //使用异步函数封装三个任务
    dispatch_async(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    
    NSLog(@"---end---");
}
打印结果:
  ---start---
  ---end---
  任务3---<NSThread: 0x600000070f00>{number = 5, name = (null)}
  任务2---<NSThread: 0x600000070d80>{number = 4, name = (null)}
  任务1---<NSThread: 0x608000074100>{number = 3, name = (null)}
解释
步骤图

(二)异步执行 + 串行队列

实现代码:
//异步执行 + 串行队列
- (void)asyncSerial{
    //创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("标识符", DISPATCH_QUEUE_SERIAL);
    
    NSLog(@"---start---");
    //使用异步函数封装三个任务
    dispatch_async(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
}
打印结果:
 ---start---
 ---end---
任务1---<NSThread: 0x608000078480>{number = 3, name = (null)}
任务2---<NSThread: 0x608000078480>{number = 3, name = (null)}
任务3---<NSThread: 0x608000078480>{number = 3, name = (null)}
解释
步骤图

(三)同步执行 + 并行队列

实现代码:
//同步执行 + 并行队列
- (void)syncConcurrent{
    //创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("标识符", DISPATCH_QUEUE_CONCURRENT);
    
    NSLog(@"---start---");
    //使用同步函数封装三个任务
    dispatch_sync(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
}
打印结果:
  ---start---
  任务1---<NSThread: 0x608000065400>{number = 1, name = main}
  任务2---<NSThread: 0x608000065400>{number = 1, name = main}
  任务3---<NSThread: 0x608000065400>{number = 1, name = main}
  ---end---
解释
步骤图

(四)同步执行+ 串行队列

实现代码:
- (void)syncSerial{
    //创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("标识符", DISPATCH_QUEUE_SERIAL);
    
    NSLog(@"---start---");
    //使用异步函数封装三个任务
    dispatch_sync(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
}
打印结果:
  ---start---
  任务1---<NSThread: 0x608000065400>{number = 1, name = main}
  任务2---<NSThread: 0x608000065400>{number = 1, name = main}
  任务3---<NSThread: 0x608000065400>{number = 1, name = main}
  ---end---
解释

(五)异步执行+主队列

实现代码:
- (void)asyncMain{
    //获取主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    NSLog(@"---start---");
    //使用异步函数封装三个任务
    dispatch_async(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
}
打印结果:
  ---start---
  ---end---
  任务1---<NSThread: 0x60800006ff40>{number = 1, name = main}
  任务2---<NSThread: 0x60800006ff40>{number = 1, name = main}
  任务3---<NSThread: 0x60800006ff40>{number = 1, name = main}
解释
步骤图

(六)同步执行+主队列(死锁)

实现代码:
- (void)syncMain{
    //获取主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    NSLog(@"---start---");
    //使用同步函数封装三个任务
    dispatch_sync(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
}
打印结果:
  ---start---
解释
步骤图

写在结尾的话

以上就是我对GCD的基础知识和几种组合的理解,如果觉得我的博客写得还可以,欢迎关注我的博客,本人将长期为大家推出高质量的技术博客。当然,如果觉得我哪里理解有错的,也可以留下你的评论。

上一篇 下一篇

猜你喜欢

热点阅读