详解IOS串行队列与并行队列进行同步或者异步的实例

 更新时间:2017年07月28日 11:54:20   作者:aron1992  
这篇文章主要介绍了详解IOS串行队列与并行队列进行同步或者异步的实例的相关资料,IOS中GCD的队列分为串行队列和并行队列,任务分为同步任务和异步任务,他们的排列组合有四种情况这里就一一分析下,需要的朋友可以参考下

详解IOS串行队列与并行队列进行同步或者异步的实例

IOS中GCD的队列分为串行队列和并行队列,任务分为同步任务和异步任务,他们的排列组合有四种情况,下面分析这四种情况的工作方式。

同步任务,使用GCD dispatch_sync 进行派发任务

- (void)testSync {
  dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL);
  dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT);

  NSLog(@"====serialQueue====");
  for (int i = 0; i<10; i++) {
    dispatch_sync(serialQueue, ^{
      [NSThread sleepForTimeInterval:0.3];
      NSLog(@"==>%@ sync serial XXX>%d", [NSThread currentThread], i);
    });
  }

  NSLog(@"====concurrentQueue====");
  for (int i = 0; i<10; i++) {
    dispatch_sync(concurrentQueue, ^{
      [NSThread sleepForTimeInterval:0.3];
      NSLog(@"==>%@ sync concurrent ====>%d", [NSThread currentThread], i*i);
    });
  }
}

结果如下:

2017-03-01 01:36:22.835 Demo ====serialQueue====
2017-03-01 01:36:23.207 {number = 1, name = main} sync serial XXX>0
2017-03-01 01:36:23.578 {number = 1, name = main} sync serial XXX>1
2017-03-01 01:36:23.952 {number = 1, name = main} sync serial XXX>2
2017-03-01 01:36:24.325 {number = 1, name = main} sync serial XXX>3
2017-03-01 01:36:24.699 {number = 1, name = main} sync serial XXX>4
2017-03-01 01:36:25.072 {number = 1, name = main} sync serial XXX>5
2017-03-01 01:36:25.446 {number = 1, name = main} sync serial XXX>6
2017-03-01 01:36:25.746 {number = 1, name = main} sync serial XXX>7
2017-03-01 01:36:26.122 {number = 1, name = main} sync serial XXX>8
2017-03-01 01:36:26.489 {number = 1, name = main} sync serial XXX>9
2017-03-01 01:36:26.489 Demo ====concurrentQueue====
2017-03-01 01:36:26.864 {number = 1, name = main} sync concurrent ====>0
2017-03-01 01:36:27.236 {number = 1, name = main} sync concurrent ====>1
2017-03-01 01:36:27.611 {number = 1, name = main} sync concurrent ====>4
2017-03-01 01:36:27.985 {number = 1, name = main} sync concurrent ====>9
2017-03-01 01:36:28.354 {number = 1, name = main} sync concurrent ====>16
2017-03-01 01:36:28.726 {number = 1, name = main} sync concurrent ====>25
2017-03-01 01:36:29.100 {number = 1, name = main} sync concurrent ====>36
2017-03-01 01:36:29.474 {number = 1, name = main} sync concurrent ====>49
2017-03-01 01:36:29.849 {number = 1, name = main} sync concurrent ====>64
2017-03-01 01:36:30.223 {number = 1, name = main} sync concurrent ====>81

testSync方法是在主线程中调用的,结果看到使用的串行队列和使用并行队列看到的结果都是发生在当前线程:主线程中,没有开启新的线程处理任务,任务的调度也是串行调度的。

异步任务,使用GCD dispatch_async 进行派发任务

- (void)testAsync {
  dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL);
  dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT);

  NSLog(@"====serialQueue====");
  for (int i = 0; i<10; i++) {
    dispatch_async(serialQueue, ^{
      [NSThread sleepForTimeInterval:0.3];
      NSLog(@"==>%@ async serial XXX>%d", [NSThread currentThread], i);
    });
  }

  NSLog(@"====concurrentQueue====");
  for (int i = 0; i<10; i++) {
    dispatch_async(concurrentQueue, ^{
      [NSThread sleepForTimeInterval:0.3];
      NSLog(@"==>%@ async concurrent ====>%d", [NSThread currentThread], i*i);
    });
  }
}
`

结果如下:

2017-03-01 01:45:36.125 Demo ====serialQueue====
2017-03-01 01:45:36.125 Demo ====concurrentQueue====
2017-03-01 01:45:36.494 {number = 3, name = (null)} async concurrent ====>0
2017-03-01 01:45:36.494 {number = 5, name = (null)} async concurrent ====>4
2017-03-01 01:45:36.494 {number = 4, name = (null)} async concurrent ====>1
2017-03-01 01:45:36.494 {number = 6, name = (null)} async concurrent ====>16
2017-03-01 01:45:36.494 {number = 8, name = (null)} async serial XXX>0
2017-03-01 01:45:36.494 {number = 7, name = (null)} async concurrent ====>9
2017-03-01 01:45:36.494 {number = 9, name = (null)} async concurrent ====>25
2017-03-01 01:45:36.494 {number = 11, name = (null)} async concurrent ====>49
2017-03-01 01:45:36.494 {number = 10, name = (null)} async concurrent ====>36
2017-03-01 01:45:36.501 {number = 13, name = (null)} async concurrent ====>81
2017-03-01 01:45:36.501 {number = 12, name = (null)} async concurrent ====>64
2017-03-01 01:45:36.869 {number = 8, name = (null)} async serial XXX>1
2017-03-01 01:45:37.244 {number = 8, name = (null)} async serial XXX>2
2017-03-01 01:45:37.615 {number = 8, name = (null)} async serial XXX>3
2017-03-01 01:45:37.986 {number = 8, name = (null)} async serial XXX>4
2017-03-01 01:45:38.358 {number = 8, name = (null)} async serial XXX>5
2017-03-01 01:45:38.730 {number = 8, name = (null)} async serial XXX>6
2017-03-01 01:45:39.103 {number = 8, name = (null)} async serial XXX>7
2017-03-01 01:45:39.472 {number = 8, name = (null)} async serial XXX>8
2017-03-01 01:45:39.842 {number = 8, name = (null)} async serial XXX>9

testSync方法是在主线程中调用的,结果看到使用的串行队列的异步任务会开启一个子线程执行任务,任务的调度是串行的
使用并行队列的异步任务会开启多个子线程并行的处理任务,任务的先后顺序是不固定的,任务的调度方式是并行的

总结

同步任务:和使用的队列无关,不会开启子线程处理任务,会在当前的线程中串行的调度任务,即一个任务完成之后继续下一个任务,如果同步任务在主线程中调用,会阻塞主线程

异步任务:a. 使用串行队列,会开启一个子线程串行的调度任务 b. 使用并行队列,会开启多个子线程并行的调度任务,这种情况用的是最多的。

以上就是对详解IOS串行队列与并行队列进行同步或者异步的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • iOS指纹验证TouchID应用学习教程

    iOS指纹验证TouchID应用学习教程

    这篇文章主要为大家详细iOS指纹验证TouchID应用学习教程的第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • iOS开发之UIScrollView详解

    iOS开发之UIScrollView详解

    UIScrollView使用非常广,本文研究UIScrollView各属性和方法,明白它们的意义、作用。这里我们整理UIScrollView一些常见用法以及一些效果的实现思路。
    2016-04-04
  • iOS中valueForKeyPath的常用方法法示例

    iOS中valueForKeyPath的常用方法法示例

    这篇文章主要给大家介绍了关于iOS中valueForKeyPath的常用方法法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • IOS LaunchScreen设置启动图片与启动页停留时间详解

    IOS LaunchScreen设置启动图片与启动页停留时间详解

    这篇文章主要介绍了IOS LaunchScreen设置启动图片与启动页停留时间详解的相关资料,需要的朋友可以参考下
    2017-02-02
  • iOS开发中用imageIO渐进加载图片及获取exif的方法

    iOS开发中用imageIO渐进加载图片及获取exif的方法

    这篇文章主要介绍了iOS开发中中用imageIO渐进加载图片及获取exif的方法,代码演示为传统的Objective-C,需要的朋友可以参考下
    2015-09-09
  • Objective-C 自定义渐变色Slider的实现方法

    Objective-C 自定义渐变色Slider的实现方法

    系统提供UISlider,但在开发过程中经常需要自定义,本次需求内容是实现一个拥有渐变色的滑动条,且渐变色随着手指touch的位置不同改变区域,这篇文章主要介绍了Objective-C 自定义渐变色Slider,需要的朋友可以参考下
    2024-07-07
  • IOS 中KVC的使用方法实例详解

    IOS 中KVC的使用方法实例详解

    这篇文章主要介绍了IOS 中KVC的使用方法实例详解的相关资料,这提供具体使用方法,希望能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • iOS 汉字的拼音

    iOS 汉字的拼音

    本文通过一段代码给代码介绍了ios汉字的拼音知识及将汉字转拼音的代码,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-08-08
  • iOS开发之如何通过PUT请求上传数据

    iOS开发之如何通过PUT请求上传数据

    众所周知一般的服务器上传数据都是用POST请求,这样通过AFNetworking的POST请求稳稳的,但是有一天遇到一个问题,服务器上传数据用的是PUT请求,发现用AFNetworking并不是那么好用,下面这篇文章就来讲一下如何通过PUT请求上传数据。有需要的朋友们可以参考借鉴。
    2016-11-11
  • iOS开发中CALayer使用的基本教程

    iOS开发中CALayer使用的基本教程

    这篇文章主要介绍了iOS开发中CALayer使用的基本教程,代码基于传统的Objective-C,需要的朋友可以参考下
    2015-11-11

最新评论