iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合
通过对前面两偏线程理解的总结,自己对线程的理解也逐渐加深,梳理的清晰起来……
通常在使用线程 的时候,都是要用到 执行对列,执行方式,执行任务,
现在开始新一轮的深入
3. 1. 1 同步 + 串行
- (void)syncSerialQueue{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@" 同步 + 串行 start:::%@ ",[NSThread currentThread]);
dispatch_sync(queue, ^{ // 添加任务 1
for (int i = 0; i < 3; i++) {
NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{ // 添加任务 2
for (int i = 10; i < 13; i++) {
NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]);
}
});
NSLog(@"同步 + 串行 end :::%@",[NSThread currentThread]);
}
执行结果:::
2017-12-21 09:10:16.725075+0800 DeadThread[10455:3327379] 同步 + 串行 start:::<NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725222+0800 DeadThread[10455:3327379] 同步 + 串行 index 0 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725339+0800 DeadThread[10455:3327379] 同步 + 串行 index 1 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725530+0800 DeadThread[10455:3327379] 同步 + 串行 index 2 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725615+0800 DeadThread[10455:3327379] 同步 + 串行 index 10 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725765+0800 DeadThread[10455:3327379] 同步 + 串行 index 11 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725821+0800 DeadThread[10455:3327379] 同步 + 串行 index 12 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main}
2017-12-21 09:10:16.725870+0800 DeadThread[10455:3327379] 同步 + 串行 end :::<NSThread: 0x60800006b4c0>{number = 1, name = main}
总结结果:::
1. 同步 : 在当前线程执行,不开启新的线程,任务顺序执行
2. 串行 :添加的任务 顺序排列,顺序执行
图示::::

3.1.2 同步 + 并行
- (void)syncConcurrentQueue{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"同步 + 并行 start:::%@ ",[NSThread currentThread]);
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"同步 + 并行 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"同步 + 并行 index %d ::: %@",i,[NSThread currentThread]);
}
});
NSLog(@"同步 + 并行 end :::%@",[NSThread currentThread]);
}
执行结果:::
2017-12-21 09:37:05.376797+0800 DeadThread[10595:3430843] 同步 + 并行 start:::<NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.376915+0800 DeadThread[10595:3430843] 同步 + 并行 index 0 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377000+0800 DeadThread[10595:3430843] 同步 + 并行 index 1 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377101+0800 DeadThread[10595:3430843] 同步 + 并行 index 2 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377262+0800 DeadThread[10595:3430843] 同步 + 并行 index 10 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377372+0800 DeadThread[10595:3430843] 同步 + 并行 index 11 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377440+0800 DeadThread[10595:3430843] 同步 + 并行 index 12 ::: <NSThread: 0x6040000759c0>{number = 1, name = main}
2017-12-21 09:37:05.377514+0800 DeadThread[10595:3430843] 同步 + 并行 end :::<NSThread: 0x6040000759c0>{number = 1, name = main}
总结结果:::
1. 同步 : 在当前线程执行,不开启新的线程,任务顺序执行
2. 并行 :添加的任务 不是顺序排列
图示::::

3.2.1 异步 + 串行
- (void)asyncSerialQueue{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"异步 + 串行 start:::%@ ",[NSThread currentThread]);
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"异步 + 串行 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"异步 + 串行 index %d ::: %@",i,[NSThread currentThread]);
}
});
NSLog(@"异步 + 串行 end :::%@",[NSThread currentThread]);
}
执行结果::::
2017-12-21 09:43:04.903888+0800 DeadThread[10647:3468013] 异步 + 串行 start:::<NSThread: 0x60c0000654c0>{number = 1, name = main}
2017-12-21 09:43:04.904032+0800 DeadThread[10647:3468013] 异步 + 串行 end :::<NSThread: 0x60c0000654c0>{number = 1, name = main}
2017-12-21 09:43:04.904056+0800 DeadThread[10647:3468266] 异步 + 串行 index 0 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
2017-12-21 09:43:04.904127+0800 DeadThread[10647:3468266] 异步 + 串行 index 1 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
2017-12-21 09:43:04.904180+0800 DeadThread[10647:3468266] 异步 + 串行 index 2 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
2017-12-21 09:43:04.904370+0800 DeadThread[10647:3468266] 异步 + 串行 index 10 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
2017-12-21 09:43:04.904534+0800 DeadThread[10647:3468266] 异步 + 串行 index 11 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
2017-12-21 09:43:04.904603+0800 DeadThread[10647:3468266] 异步 + 串行 index 12 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}
总结结果:::
1.异步: 开启新的线程,不影响当前线程;
2.串行: 添加 任务 顺序排列,顺序执行

3.2.2 异步 + 并行
- (void)asyncConcurrentQueue{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"异步 + 并行 start:::%@ ",[NSThread currentThread]);
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"异步 + 并行 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"异步 + 并行 index %d ::: %@",i,[NSThread currentThread]);
}
});
NSLog(@"同步 + 并行 end :::%@",[NSThread currentThread]);
}
执行结果:::
2017-12-21 09:50:40.207852+0800 DeadThread[10727:3517966] 异步 + 并行 start:::<NSThread: 0x60c00006d880>{number = 1, name = main}
2017-12-21 09:50:40.208038+0800 DeadThread[10727:3517966] 同步 + 并行 end :::<NSThread: 0x60c00006d880>{number = 1, name = main}
2017-12-21 09:50:40.208045+0800 DeadThread[10727:3518055] 异步 + 并行 index 10 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)}
2017-12-21 09:50:40.208066+0800 DeadThread[10727:3518052] 异步 + 并行 index 0 ::: <NSThread: 0x600000075700>{number = 4, name = (null)}
2017-12-21 09:50:40.208139+0800 DeadThread[10727:3518055] 异步 + 并行 index 11 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)}
2017-12-21 09:50:40.208197+0800 DeadThread[10727:3518052] 异步 + 并行 index 1 ::: <NSThread: 0x600000075700>{number = 4, name = (null)}
2017-12-21 09:50:40.208327+0800 DeadThread[10727:3518055] 异步 + 并行 index 12 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)}
2017-12-21 09:50:40.208361+0800 DeadThread[10727:3518052] 异步 + 并行 index 2 ::: <NSThread: 0x600000075700>{number = 4, name = (null)}
总结结果::::
1.异步:开启线程能力;
2.并行:任务队列不顺序排列,同时执行;
3.开启线程的数量:取决于添加任务的数量
图示:::

iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合的更多相关文章
- iOS 多线程的简单理解(1) 方式 :同步 异步
最近遇到特别糟糕的面试,过程中提到多次对多线程的处理问题,并没有很好的给予答复和解决,所以在这里做个简单的备案: 期望能更加了解和熟练使用 多线程技术: 下面都是自己的总结,如果存在不对的,或者不足, ...
- iOS 多线程的简单理解(4) 线程锁的简单使用
要用到多线程 ,就不得不考虑,线程之间的交互,线程是否安全 推荐一个原文链接 是关于 线程锁的基本使用的 http://blog.csdn.net/qq_30513483/article/detai ...
- iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue
多线程队列是装载线程任务的队形结构.(系统以先进先出的方式调度队列中的任务执行 FIFO).在GCD中有两种队列: 串行队列.并发队列. 队列 :串行队列.并发队列,全局主对列,全局并发队列 2.1. ...
- ios多线程开发的常用三种方式
1.NSThread 2.NSOperationQueue 3.GCD NSThread: 创建方式主要有两种: [NSThread detachNewThreadSelector:@selector ...
- C#多线程的简单理解
一.CLR线程池基础 创建和销毁线程是一个昂贵的操作,所以CLR管理了一个线程池(thread pool),可以将线程池看成一个黑盒. CLR初始化时,线程池中是没有线程的.线程的初始化与其他线程一样 ...
- ios -RunLoop(简单理解)
一. RunLoop简介 RunLoop字面意思是运行时,即跑圈得意思.它可以在我们需要的时候自己跑起来运行,在我们没有操作的时候就停下来休息,充分节省CPU资源,提高程序性能. 二. RunLoop ...
- iOS NSRunloop的简单理解
最近学习了下NSRunloop. 作一下简单的理解: 1.runloop与线程的关系,每一个线程创建是都会有伴有一个runloop诞生,runloop用来接收事件源,让线程执行事件.当没有事件处理时, ...
- iOS On-Demand Resources简单理解
ios9引入了一个新功能,On-Demand Resources,它是app thinning 的一部分.这个机能简单的说,就是在下载app的时候,app中包含的不重要资源不下载,等到需要时,在由系统 ...
- iOS 块的简单理解
占位 自己主动转载器那小子,你转完了没? 转完了,我开写了哈! Block,就两个事儿,一个是引用,一个是实例,除了实现处.其他地方都是引用. 以此思路.再继续看看引用和实现的定义方式吧. 參考官方文 ...
随机推荐
- Kubernetes 学习22 kubernetes容器资源需求资源限制及HeapSter(翻车章节)
一.概述 1.接下来介绍在k8s上运行pod对象时我们如何去监控我们系统级的资源指标以及业务级别的资源指标.数据如何获取和监控.在此之前先介绍一下Pod对象的资源请求和资源限制.即容器的资源需求和资源 ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- DIJ的优化,和spfa的优化
SPFA和DIJ求最短路的算法的坑点一直是很多的.经常会让人搞不懂. 易错案例: 用重载运算符来排序,如: struct cmp { bool operator ()(int x, int y) co ...
- KillTimer不能放在析构函数,可以放在DestroyWindow函数里
转自 https://www.cnblogs.com/huking/archive/2009/11/27/1612201.html KillTimer&析构函数 析构函数中不能用KillTim ...
- Python和多线程(multi-threading)。这是个好主意码?列举一些让Python代码以并行方式运行的方法。
Python并不支持真正意义上的多线程.Python中提供了多线程包,但是如果你想通过多线程提高代码的速度,使用多线程包并不是个好主意.Python中有一个被称为Global Interpreter ...
- SpringData like关键字不起作用
使用springdata简单查询时,like关键字不起作用 Hibernate: select article0_.oId as oId1_2_, article0_.articleAbstract ...
- UML图中时序图的基本用法
快速阅读 序列图主要用来更直观的表现各个对象交互的时间顺序,将体现的重点放在 以时间为参照,各个对象发送.接收消息,处理消息,返回消息的 时间流程顺序,也称为时序图. 里面用到的基本元素如下: 角色- ...
- 2018-2019 20165226 Exp9 Web安全基础
2018-2019 20165226 Exp9 Web安全基础 目录 一.实验内容说明及基础问题回答 二.实验过程 Webgoat准备 XSS攻击 ① Phishing with XSS 跨站脚本钓鱼 ...
- certification on windows and
https://jingyan.baidu.com/article/335530dae0eb2319ca41c378.html
- 在Ubuntu下安装VWMare tools
之前随便解压在一个目录下一直不能安装,后来把压缩包解压到home目录下就可以了. 详细步骤:https://jingyan.baidu.com/article/597a0643356fdc312b52 ...