年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 279036|回复: 1

多线程技术GCD的8种队列请求示例

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-11-8 15:27:04 | 显示全部楼层 |阅读模式
    1、串行队列同步请求
    1. #pragma mark 1、串行队列同步请求
    2. - (void)gcd1
    3. {
    4.     // 创建一个串行队列dispatch_queue_create(标识, 串行队列),DISPATCH_QUEUE_SERIAL 表示串行队列
    5.     dispatch_queue_t queue = dispatch_queue_create("gcd1", DISPATCH_QUEUE_SERIAL);
    6.    
    7.    
    8.     // 发起一个同步任务并执行10次:dispatch_sync(dispatch_queue_t queue, ^{ code })是同步执行函数
    9.     for (int i = 0; i < 10; i++) {
    10.         
    11.         dispatch_sync(queue, ^{
    12.             // 打印当前线程
    13.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    14.         });
    15.     }
    16.     // 结论:串行任务按顺序执行,同步任务按顺序执行,无并发数,当前只有一个线程,因此在线程1按顺序执行
    17. }
    复制代码
    运行结果:
    2014-11-08 15:12:37.952 Test[3166:607] 第 0 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.955 Test[3166:607] 第 1 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.957 Test[3166:607] 第 2 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.958 Test[3166:607] 第 3 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.959 Test[3166:607] 第 4 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.959 Test[3166:607] 第 5 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.960 Test[3166:607] 第 6 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.961 Test[3166:607] 第 7 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.965 Test[3166:607] 第 8 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}
    2014-11-08 15:12:37.966 Test[3166:607] 第 9 次 - <NSThread: 0x8c02f70>{name = (null), num = 1}

    2、串行队列异步请求
    1. #pragma mark 2、串行队列异步请求
    2. - (void)gcd2
    3. {
    4.     // 创建一个串行队列dispatch_queue_create(标识, 串行队列),DISPATCH_QUEUE_SERIAL 表示串行队列
    5.     dispatch_queue_t queue = dispatch_queue_create("gcd2", DISPATCH_QUEUE_SERIAL);
    6.    
    7.     // 发起一个异步任务并执行10次:dispatch_async(dispatch_queue_t queue, ^{ code })是异步执行函数
    8.     for (int i = 0; i < 10; i++) {
    9.         
    10.         dispatch_async(queue, ^{
    11.             // 打印当前线程
    12.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    13.         });
    14.     }
    15.     // 结论:异步任务可多个同时进行,无先后顺序,且另开线程并发进行,但由于是串行队列,只能在单个线程中完成,因此在线程2按顺序执行
    16. }
    复制代码
    运行结果:
    2014-11-08 15:14:35.006 Test[3207:1303] 第 0 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.008 Test[3207:1303] 第 1 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.011 Test[3207:1303] 第 2 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.012 Test[3207:1303] 第 3 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.023 Test[3207:1303] 第 4 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.024 Test[3207:1303] 第 5 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.025 Test[3207:1303] 第 6 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.026 Test[3207:1303] 第 7 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.028 Test[3207:1303] 第 8 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}
    2014-11-08 15:14:35.032 Test[3207:1303] 第 9 次 - <NSThread: 0x8c55cc0>{name = (null), num = 2}


    3、并行队列异步请求
    1. #pragma mark 3、并行队列同步请求
    2. - (void)gcd3
    3. {
    4.     // 创建一个并行队列dispatch_queue_create(标识, 串行队列),DISPATCH_QUEUE_CONCURRENT 表示并行队列
    5.     dispatch_queue_t queue = dispatch_queue_create("gcd3", DISPATCH_QUEUE_CONCURRENT);
    6.    
    7.     // 发起一个同步任务并执行10次:dispatch_sync(dispatch_queue_t queue, ^{ code })是同步执行函数
    8.     for (int i = 0; i < 10; i++) {
    9.         
    10.         dispatch_sync(queue, ^{
    11.             // 打印当前线程
    12.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    13.         });
    14.     }
    15.     // 结论:由于同步任务,因此只能按顺序执行,由于是并行队列,原则上可创建多个线程,但同步任务让其无法发挥作用
    16. }
    复制代码
    运行结果:
    2014-11-08 15:15:47.285 Test[3241:607] 第 0 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.287 Test[3241:607] 第 1 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.288 Test[3241:607] 第 2 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.289 Test[3241:607] 第 3 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.290 Test[3241:607] 第 4 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.292 Test[3241:607] 第 5 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.292 Test[3241:607] 第 6 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.293 Test[3241:607] 第 7 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.294 Test[3241:607] 第 8 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}
    2014-11-08 15:15:47.295 Test[3241:607] 第 9 次 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}


    4、并行队列异步请求
    1. #pragma mark 4、并行队列异步请求
    2. - (void)gcd4
    3. {
    4.     // 创建一个并行队列dispatch_queue_create(标识, 串行队列),DISPATCH_QUEUE_CONCURRENT 表示并行队列
    5.      dispatch_queue_t queue = dispatch_queue_create("gcd4", DISPATCH_QUEUE_CONCURRENT);
    6.    
    7.     // 发起一个异步任务并执行10次:dispatch_async(dispatch_queue_t queue, ^{ code })是异步执行函数
    8.     for (int i = 0; i < 10; i++) {
    9.         
    10.         dispatch_async(queue, ^{
    11.             // 打印当前线程
    12.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    13.         });
    14.     }
    15.     // 结论:由于是异步队列,for循环中没等单次循环结束直接新开线程进入下一次循环,因此线程数以及在哪个线程中完成随机,程序员不可控
    16. }
    复制代码
    运行结果:
    2014-11-08 15:17:04.004 Test[3273:3b03] 第 9 次 - <NSThread: 0x8d30120>{name = (null), num = 11}
    2014-11-08 15:17:04.001 Test[3273:3703] 第 5 次 - <NSThread: 0x8c06f70>{name = (null), num = 7}
    2014-11-08 15:17:04.001 Test[3273:3107] 第 1 次 - <NSThread: 0x8d2f2a0>{name = (null), num = 6}
    2014-11-08 15:17:04.002 Test[3273:3603] 第 4 次 - <NSThread: 0x8d2ea20>{name = (null), num = 5}
    2014-11-08 15:17:04.002 Test[3273:3803] 第 6 次 - <NSThread: 0x8d2f250>{name = (null), num = 8}
    2014-11-08 15:17:04.001 Test[3273:3403] 第 2 次 - <NSThread: 0x8d2e6b0>{name = (null), num = 3}
    2014-11-08 15:17:04.003 Test[3273:3a03] 第 8 次 - <NSThread: 0x8d2fd50>{name = (null), num = 10}
    2014-11-08 15:17:04.006 Test[3273:3503] 第 3 次 - <NSThread: 0x8d2e880>{name = (null), num = 4}
    2014-11-08 15:17:04.001 Test[3273:1303] 第 0 次 - <NSThread: 0x8c06610>{name = (null), num = 2}
    2014-11-08 15:17:04.002 Test[3273:3903] 第 7 次 - <NSThread: 0x8d2f7b0>{name = (null), num = 9}


    5、全局队列异步请求
    1. #pragma mark 5、全局队列同步请求
    2. - (void)gcd5
    3. {
    4.     // 获取全局队列 dispatch_get_global_queue(队列优先级, 预留标记)
    5.     // 有4种优先级,DISPATCH_QUEUE_PRIORITY_DEFAULT为默认优先级,预留标记永远为0,当前无具体含义
    6.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    7.    
    8.     // 发起一个同步任务并执行10次:dispatch_sync(dispatch_queue_t queue, ^{ code })是同步执行函数
    9.     for (int i = 0; i < 10; i++) {
    10.         
    11.         dispatch_sync(queue, ^{
    12.             // 打印当前线程
    13.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    14.         });
    15.     }
    16.     // 结论:全局队列与自定义队列类似,不同的是全局队列默认是并发队列,但同步请求使其只能单线程执行
    17. }
    复制代码
    运行结果:
    2014-11-08 15:18:18.991 Test[3310:607] 第 0 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.993 Test[3310:607] 第 1 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.994 Test[3310:607] 第 2 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.994 Test[3310:607] 第 3 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.994 Test[3310:607] 第 4 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.995 Test[3310:607] 第 5 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.995 Test[3310:607] 第 6 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.996 Test[3310:607] 第 7 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.996 Test[3310:607] 第 8 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}
    2014-11-08 15:18:18.997 Test[3310:607] 第 9 次 - <NSThread: 0x8e08d70>{name = (null), num = 1}


    6、全局队列异步请求
    1. #pragma mark 6、全局队列异步请求
    2. - (void)gcd6
    3. {
    4.     // 获取全局队列 dispatch_get_global_queue(队列优先级, 预留标记)
    5.     // 有4种优先级,DISPATCH_QUEUE_PRIORITY_DEFAULT为默认优先级,预留标记永远为0,当前无具体含义
    6.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    7.    
    8.     // 发起一个异步任务并执行10次:dispatch_async(dispatch_queue_t queue, ^{ code })是异步执行函数
    9.     for (int i = 0; i < 10; i++) {
    10.         
    11.         dispatch_async(queue, ^{
    12.             // 打印当前线程
    13.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    14.         });
    15.     }
    16.     // 结论:全局队列异步请求与并行队列异步请求类似,说明全局队列是并行队列。
    17. }
    复制代码
    运行结果:
    2014-11-08 15:19:30.332 Test[3337:3503] 第 3 次 - <NSThread: 0x8e3eab0>{name = (null), num = 5}
    2014-11-08 15:19:30.332 Test[3337:1303] 第 0 次 - <NSThread: 0x8e3f8d0>{name = (null), num = 2}
    2014-11-08 15:19:30.332 Test[3337:3803] 第 6 次 - <NSThread: 0x8f19530>{name = (null), num = 8}
    2014-11-08 15:19:30.332 Test[3337:3107] 第 1 次 - <NSThread: 0x8f195b0>{name = (null), num = 3}
    2014-11-08 15:19:30.332 Test[3337:3403] 第 2 次 - <NSThread: 0x8c3a140>{name = (null), num = 4}
    2014-11-08 15:19:30.332 Test[3337:3703] 第 5 次 - <NSThread: 0x8c3b1c0>{name = (null), num = 7}
    2014-11-08 15:19:30.332 Test[3337:3603] 第 4 次 - <NSThread: 0x8c3a260>{name = (null), num = 6}
    2014-11-08 15:19:30.334 Test[3337:3903] 第 7 次 - <NSThread: 0x8f19bf0>{name = (null), num = 9}
    2014-11-08 15:19:30.334 Test[3337:3c03] 第 8 次 - <NSThread: 0x8f1a500>{name = (null), num = 10}
    2014-11-08 15:19:30.335 Test[3337:3d03] 第 9 次 - <NSThread: 0x8f1a7c0>{name = (null), num = 11}


    7、主队列异步请求
    1. #pragma mark 7、主队列同步请求
    2. - (void)gcd7
    3. {
    4.     // 获取主队列 dispatch_get_main_queue()
    5.     dispatch_queue_t queue = dispatch_get_main_queue();
    6.    
    7.     NSLog(@"任务开始");
    8.     // 发起一个同步任务并执行10次:dispatch_sync(dispatch_queue_t queue, ^{ code })是同步执行函数
    9.     for (int i = 0; i < 10; i++) {
    10.         
    11.         dispatch_sync(queue, ^{
    12.             // 打印当前线程
    13.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    14.         });
    15.     }
    16.     NSLog(@"任务结束");
    17.    
    18.     // 结论:主队列同步请求不会进入循环体,因为应用程序在主队列中开启了消息循环,该任务在程序运行时不会结束,因此当前任务未完成,下一个任务永远不会开始。
    19. }
    复制代码
    运行结果:
    2014-11-08 15:20:50.754 Test[3372:607] 任务开始

    8、主队列异步请求
    1. #pragma mark 8、主队列异步请求
    2. - (void)gcd8
    3. {
    4.     // 获取主队列 dispatch_get_main_queue()
    5.     dispatch_queue_t queue = dispatch_get_main_queue();
    6.    
    7.     // 发起一个异步任务并执行10次:dispatch_async(dispatch_queue_t queue, ^{ code })是异步执行函数
    8.     for (int i = 0; i < 10; i++) {
    9.         
    10.         dispatch_async(queue, ^{
    11.             // 打印当前线程
    12.             NSLog(@"第 %d 次 - %@", i, [NSThread currentThread]);
    13.         });
    14.     }
    15.     // 结论:主队列中发起一个异步请求,只在线程1中执行,因为主队列是个串行队列
    16. }
    复制代码
    运行结果:
    2014-11-08 15:23:08.199 Test[3425:607] 第 0 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.203 Test[3425:607] 第 1 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.204 Test[3425:607] 第 2 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.205 Test[3425:607] 第 3 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.205 Test[3425:607] 第 4 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.206 Test[3425:607] 第 5 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.207 Test[3425:607] 第 6 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.210 Test[3425:607] 第 7 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.210 Test[3425:607] 第 8 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}
    2014-11-08 15:23:08.211 Test[3425:607] 第 9 次 - <NSThread: 0x8e0ba40>{name = (null), num = 1}


    Demo下载:
    游客,如果您要查看本帖隐藏内容请回复

  • TA的每日心情
    犯困
    2015-1-11 14:36
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    发表于 2015-6-26 12:00:35 | 显示全部楼层
    研究一下了
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-3 09:09 , Processed in 0.052554 second(s), 25 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表