IOS

关注公众号 jb51net

关闭
首页 > 软件编程 > IOS > iOS-GCD使用详解

iOS-GCD使用详解及实例解析

投稿:lqh

这篇文章主要介绍了iOS-GCD使用详解及实例解析的相关资料,需要的朋友可以参考下

iOS-GCD使用详解

前言

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

线程、任务和队列的概念

1.png

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

2.png

一条重要的准则

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

3.png

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

实现代码:

//异步执行 + 并行队列
- (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---");
}

 打印结果:

1 2 3 4 5 ---start---   ---end---   任务3---{number = 5, name = (null)}   任务2---{number = 4, name = (null)}   任务1---{number = 3, name = (null)}

解释

 步骤图

4.png

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

实现代码:

//异步执行 + 串行队列
- (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---");
}

 打印结果:

1 2 3 4 5  ---start---  ---end--- 任务1---{number = 3, name = (null)} 任务2---{number = 3, name = (null)} 任务3---{number = 3, name = (null)}

 

解释

步骤图

5.png

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

实现代码:

//同步执行 + 并行队列
- (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---");
}

 打印结果:

1 2 3 4 5 ---start---   任务1---{number = 1, name = main}   任务2---{number = 1, name = main}   任务3---{number = 1, name = main}   ---end---

解释

步骤图

6.png

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

实现代码:

- (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---");
}

 打印结果:

1 2 3 4 5   ---start---   任务1---{number = 1, name = main}   任务2---{number = 1, name = main}   任务3---{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---");
}

 打印结果:

1 2 3 4 5   ---start---   ---end---   任务1---{number = 1, name = main}   任务2---{number = 1, name = main}   任务3---{number = 1, name = main}

 解释

步骤图

7.png

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

实现代码:

- (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---");
}

 打印结果:

1   ---start---

解释

步骤图

8.png

 写在结尾的话

以上就是我对GCD的基础知识和几种组合的理解。

您可能感兴趣的文章:
阅读全文