当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS-GCD详解及简单使用

iOS-GCD详解及简单使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

北方影院换心,邪恶动态图片27,手机充值卡进货渠道

ios-gcd 介绍

在开发过程中,我们有时会希望把一些操作封装起来延迟一段时间后再执行。ios开发中,有两种常用的方法可以实现延迟执行,一种是使用gcd,另外一种是使用nsrunloop类中提供的方法。

前言

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

线程、任务和队列的概念

1.png

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

2.png

一条重要的准则

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

  1. 能开启新的线程
  2. 任务可以同时执行
  3. 结合以上两个条件,也就等价“开启新线程的能力 + 任务同步执行的权利”,只有在满足能力与权利这两个条件的前提下,我们才可以在同时执行多个任务。

 所有组合的特点

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

打印结果:

---start---

  ---end---

  任务3---{number = 5, name = (null)}

  任务2---{number = 4, name = (null)}

  任务1---{number = 3, name = (null)} 

解释:

1.异步执行意味着

可以开启新的线程

任务可以先绕过不执行,回头再来执行

2.并行队列意味着

任务之间不需要排队,且具有同时被执行的“权利”

3.两者组合后的结果

开了三个新线程

函数在执行时,先打印了start和end,再回头执行这三个任务

这三个任务是同时执行的,没有先后,所以打印结果是“任务3-->任务2-->任务1”

步骤图

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

打印结果:

 ---start---

 ---end---

任务1---{number = 3, name = (null)}

任务2---{number = 3, name = (null)}

任务3---{number = 3, name = (null)}

解释:

异步执行意味着

可以开启新的线程

任务可以先绕过不执行,回头再来执行

串行队列意味着

任务必须按添加进队列的顺序挨个执行

两者组合后的结果

开了一个新的子线程

函数在执行时,先打印了start和end,再回头执行这三个任务

这三个任务是按顺序执行的,所以打印结果是“任务1-->任务2-->任务3”

步骤图

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

打印结果:

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

打印结果:

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

打印结果:

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

打印结果:

  ---start---

解释

  1. 主队列中的任务必须按顺序挨个执行
  2. 任务1要等主线程有空的时候(即主队列中的所有任务执行完)才能执行
  3. 主线程要执行完“打印end”的任务后才有空
  4. “任务1”和“打印end”两个任务互相等待,造成死锁

步骤图

8.png

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网