iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例
2017-09-02 23:21
721 查看
多线程05CGD同步异步函数和并行串行主队列示例
一说明与图示
二异步函数 并发队列
1 代码
2 结果
三异步函数 串行队列
1 代码示例
2 结果
四同步函数 并发队列
1 代码示例
2 结果
五同步函数 串行队列
1 代码示例
2 结果
六异步函数 主队列
1 代码示例
2 结果
七同步函数 主队列 死锁除非该方法在子线程中执行
1 代码示例
2 结果
八同步函数子线程 主队列
1 代码示例
2 结果
1.异步函数 + 并发队列 —— 会开启多条线程,队列中的任务是并发执行
2.异步函数 + 串行队列 —— 会开线程,开一条线程,队列中的任务是串行执行的
3.同步函数 + 并发队列 —— 不会开线程,任务是串行执行的
4.同步函数 + 串行队列 —— 不会开线程,任务是串行执行的
5.异步函数 + 主队列 —— 所有任务都在主线程中执行,不会开线程
6.同步函数 + 主队列 —— 死锁。除非该方法在子线程中执行
7.同步函数(子线程) + 主队列
特别说明:
使用sync函数往当前串行队列中添加任务,会卡住当前的串行队列(死锁
图示:
一说明与图示
二异步函数 并发队列
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}
相关文章推荐
- iOS学习笔记74-IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别附代码
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等(有示例代码)
- iOS GCD/主队列/并行队列/全局队列/串行队列/同步任务/异步任务区别 含代码
- 详解IOS串行队列与并行队列进行同步或者异步的实例
- iOS 异步同步执行和并行串行队列的不解之缘
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- iOS学习笔记-111.多线程10——CGD队列组图片下载合成示例
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- ios--进程/多线程/同步任务/异步任务/串行队列/并行队列(对比分析)
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等
- iOS GCD, 同步,异步,串行队列,并行队列,dispatch_group
- iOS -- 进程/多线程/同步任务/异步任务/串行队列/并行队列(对比分析)
- iOS-线程之GCD方式---之同步异步和串行队列并行队列之间的关系
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- IOS串行队列、并行队列进行同步或者异步任务解析
- IOS串行队列、并行队列进行同步或者异步任务解析
- ios-day19-02(GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务)
- iOS:对GCD中 同步、异步、并行、串行的见解
- 【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例