iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例
来源:互联网 发布:智能优化算法的优缺点 编辑:程序博客网 时间:2024/05/17 21:44
- 多线程05CGD同步异步函数和并行串行主队列示例
- 一说明与图示
- 二异步函数 并发队列
- 1 代码
- 2 结果
- 三异步函数 串行队列
- 1 代码示例
- 2 结果
- 四同步函数 并发队列
- 1 代码示例
- 2 结果
- 五同步函数 串行队列
- 1 代码示例
- 2 结果
- 六异步函数 主队列
- 1 代码示例
- 2 结果
- 七同步函数 主队列 死锁除非该方法在子线程中执行
- 1 代码示例
- 2 结果
- 八同步函数子线程 主队列
- 1 代码示例
- 2 结果
多线程05——CGD同步、异步函数和并行、串行、主队列示例
一、说明与图示
我们主要完成以下的主要示例
1.异步函数 + 并发队列 —— 会开启多条线程,队列中的任务是并发执行
2.异步函数 + 串行队列 —— 会开线程,开一条线程,队列中的任务是串行执行的
3.同步函数 + 并发队列 —— 不会开线程,任务是串行执行的
4.同步函数 + 串行队列 —— 不会开线程,任务是串行执行的
5.异步函数 + 主队列 —— 所有任务都在主线程中执行,不会开线程
6.同步函数 + 主队列 —— 死锁。除非该方法在子线程中执行
7.同步函数(子线程) + 主队列
特别说明:
使用sync函数往当前串行队列中添加任务,会卡住当前的串行队列(死锁
图示:
二、异步函数 + 并发队列
我们点击对应按钮的时候去执行这个操作
2.1 代码
- (IBAction)acClick:(id)sender { NSLog(@"--------acClick---------"); [self asyncConcurrent];}/* 1.异步函数 + 并发队列*/-(void)asyncConcurrent{ //1. 获取队列 //第一个参数:c语言的字符串,标签 //第二个参数:队列的类型 // DISPATCH_QUEUE_CONCURRENT 并发 // DISPATCH_QUEUE_SERIAL 串行 dispatch_queue_t queue = dispatch_queue_create("com.wiming.asyncConcurrent",DISPATCH_QUEUE_CONCURRENT); //2.封装任务>添加任务到队列中 dispatch_async(queue, ^{ NSLog(@"asyncConcurrent---download1-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncConcurrent---download2-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncConcurrent---download3-----%@",[NSThread currentThread]); });}
2.2 结果
[57261:318823] --------acClick---------[57261:326249] asyncConcurrent---download1-----<NSThread: 0x608000278840>{number = 6, name = (null)}[57261:326263] asyncConcurrent---download2-----<NSThread: 0x60000026ae00>{number = 7, name = (null)}[57261:326264] asyncConcurrent---download3-----<NSThread: 0x60000026a540>{number = 8, name = (null)}
三、异步函数 + 串行队列
3.1 代码示例
- (IBAction)asClick:(id)sender { NSLog(@"--------asClick---------"); [self asyncSerial];}/* 2.异步函数 + 串行队列 */-(void)asyncSerial{ dispatch_queue_t queue = dispatch_queue_create("com.wiming.asyncSerial", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ NSLog(@"asyncSerial---download1-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncSerial---download2-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncSerial---download3-----%@",[NSThread currentThread]); });}
3.2 结果
[57261:318823] --------asClick---------[57261:327801] asyncSerial---download1-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}[57261:327801] asyncSerial---download2-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}[57261:327801] asyncSerial---download3-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}
四、同步函数 + 并发队列
4.1 代码示例
- (IBAction)scClick:(id)sender { NSLog(@"--------scClick---------"); [self syncConcurrent];}/* 3.同步函数 + 并发队列 */-(void)syncConcurrent{ dispatch_queue_t queue = dispatch_queue_create("com.wiming.syncConcurrent", DISPATCH_QUEUE_CONCURRENT); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download1-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download2-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download3-----%@",[NSThread currentThread]); });}
4.2 结果
[57261:318823] --------scClick---------[57261:318823] syncConcurrent---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download3-----<NSThread: 0x608000263600>{number = 1, name = main}
五、同步函数 + 串行队列
5.1 代码示例
- (IBAction)ssClick:(id)sender { NSLog(@"--------ssClick---------"); [self syncSerial];}/* 4.同步函数 + 串行队列*/-(void)syncSerial{ dispatch_queue_t queue = dispatch_queue_create("com.wiming.syncConcurrent", DISPATCH_QUEUE_SERIAL); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download1-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download2-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncConcurrent---download3-----%@",[NSThread currentThread]); });}
5.2 结果
[57261:318823] --------ssClick---------[57261:318823] syncConcurrent---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download3-----<NSThread: 0x608000263600>{number = 1, name = main}
六、异步函数 + 主队列
6.1 代码示例
- (IBAction)amClick:(id)sender { NSLog(@"--------amClick---------"); [self asyncMain];}/* 5.异步函数 + 主队列*/-(void)asyncMain{ dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ NSLog(@"asyncMain---download1-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncMain---download2-----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"asyncMain---download3-----%@",[NSThread currentThread]); });}
6.2 结果
[57261:318823] --------amClick---------[57261:318823] asyncMain---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] asyncMain---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] asyncMain---download3-----<NSThread: 0x608000263600>{number = 1, name = main}
七、同步函数 + 主队列 :死锁。除非该方法在子线程中执行
7.1 代码示例
- (IBAction)smClick:(id)sender { NSLog(@"--------smClick---------"); [self syncMain];}/* 6.同步函数 + 主队列 :死锁。除非该方法在子线程中执行*/-(void)syncMain{ dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_sync(queue, ^{ NSLog(@"syncMain---download1-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncMain---download2-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncMain---download3-----%@",[NSThread currentThread]); });}
7.2 结果
2017-09-02 23:09:45.567 03_UIview81多线程GCD[57261:318823] --------smClick---------然后程序就挂了
八、同步函数(子线程) + 主队列
8.1 代码示例
- (IBAction)smNewThreadClick:(id)sender { NSLog(@"--------smNewThreadClick---------"); [self syncMainSonThread];}/* 7.同步函数(子线程) + 主队列 */-(void)syncMainSonThread{ dispatch_queue_t queue = dispatch_get_main_queue(); [NSThread detachNewThreadWithBlock:^{ dispatch_sync(queue, ^{ NSLog(@"syncMainSonThread---download1-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncMainSonThread---download2-----%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"syncMainSonThread---download3-----%@",[NSThread currentThread]); }); }];}
8.2 结果
[57261:318823] --------smNewThreadClick---------[57261:318823] syncMainSonThread---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncMainSonThread---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncMainSonThread---download3-----<NSThread: 0x608000263600>{number = 1, name = main}
阅读全文
0 0
- iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例
- iOS学习笔记74-IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别附代码
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- GCD 主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- ios-day19-02(GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务)
- iOS GCD/主队列/并行队列/全局队列/串行队列/同步任务/异步任务区别 含代码
- GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务
- iOS学习笔记-111.多线程10——CGD队列组图片下载合成示例
- iOS学习笔记-105.多线程04——CGD基础
- iOS 主队列异步函数使用
- iOS学习笔记-108.多线程07——CGD栅栏函数、延时、一次性代码
- IOS学习 GCD 基础 串行/并行/主线程、同步/异步
- IOS 异步同步执行和并行串行队列的不解之缘
- 串行 并行 同步 异步
- 同步 异步 并行 串行
- 同步 异步 串行 并行
- 同步异步 串行 并行
- 关于多线程中同步函数+主队列的问题
- 公司MES售前思路演进
- Linux下系统自带python和Anaconda切换
- HDU
- java mock及Mockito实战
- 性能优化(1)
- iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例
- Python 3.0 面向对象
- iOS 查找Mac电脑的地址
- 冒泡排序
- leetcode动态规划总结
- 灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断
- 计算机组成知识整理3:数的表示
- php基础学习day1
- jquery-easyui常用demo