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

发布时间 - 2026-01-11 02:33:19    点击率:

详解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串行队列与并行队列进行同步或者异步  # IOS串行队列与并行队列  # IOS开发-多线程队列测试代码  # iOS应用程序中通过dispatch队列控制线程执行的方法  # 详解iOS中多线程app开发的GCD队列的使用  # ios实现简易队列  # 是在  # 多个  # 的是  # 都是  # 他们的  # 最多  # 如有  # 会在  # 希望能  # 这种情况  # 四种  # 谢谢大家  # 这四  # 疑问请  # 排列组合  # 发生在  # gt  # serial  # sync  # lt 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Chrome浏览器标签页分组怎么用_谷歌浏览器整理标签页技巧【效率】  如何快速搭建安全的FTP站点?  如何在宝塔面板创建新站点?  手机网站制作与建设方案,手机网站如何建设?  百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧  如何获取PHP WAP自助建站系统源码?  如何在云主机上快速搭建网站?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  Laravel如何创建自定义中间件?(Middleware代码示例)  香港网站服务器数量如何影响SEO优化效果?  Android中AutoCompleteTextView自动提示  如何快速搭建高效服务器建站系统?  Linux系统命令中screen命令详解  如何用已有域名快速搭建网站?  java中使用zxing批量生成二维码立牌  制作企业网站建设方案,怎样建设一个公司网站?  Laravel怎么写单元测试_PHPUnit在Laravel项目中的基础测试入门  香港服务器部署网站为何提示未备案?  如何用y主机助手快速搭建网站?  JavaScript如何实现继承_有哪些常用方法  网站制作壁纸教程视频,电脑壁纸网站?  如何在不使用负向后查找的情况下匹配特定条件前的换行符  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  Laravel如何使用.env文件管理环境变量?(最佳实践)  大学网站设计制作软件有哪些,如何将网站制作成自己app?  javascript事件捕获机制【深入分析IE和DOM中的事件模型】  什么是JavaScript解构赋值_解构赋值有哪些实用技巧  Windows Hello人脸识别突然无法使用  Laravel Telescope怎么调试_使用Laravel Telescope进行应用监控与调试  Laravel Eloquent:优雅地将关联模型字段扁平化到主模型中  Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】  如何打造高效商业网站?建站目的决定转化率  🚀拖拽式CMS建站能否实现高效与个性化并存?  Laravel如何使用Livewire构建动态组件?(入门代码)  html5源代码发行怎么设置权限_访问权限控制方法与实践【指南】  Swift开发中switch语句值绑定模式  海南网站制作公司有哪些,海口网是哪家的?  Laravel怎么清理缓存_Laravel optimize clear命令详解  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】  如何快速完成中国万网建站详细流程?  在线制作视频的网站有哪些,电脑如何制作视频短片?  简历没回改:利用AI润色让你的文字更专业  香港服务器租用费用高吗?如何避免常见误区?  Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析  微信小程序 canvas开发实例及注意事项  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  高端网站建设与定制开发一站式解决方案 中企动力  Laravel项目怎么部署到Linux_Laravel Nginx配置详解