通过对前面两偏线程理解的总结,自己对线程的理解也逐渐加深,梳理的清晰起来……

通常在使用线程 的时候,都是要用到 执行对列,执行方式,执行任务,

现在开始新一轮的深入

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)执行方式 + 执行对列 的组合的更多相关文章

  1. iOS 多线程的简单理解(1) 方式 :同步 异步

    最近遇到特别糟糕的面试,过程中提到多次对多线程的处理问题,并没有很好的给予答复和解决,所以在这里做个简单的备案: 期望能更加了解和熟练使用 多线程技术: 下面都是自己的总结,如果存在不对的,或者不足, ...

  2. iOS 多线程的简单理解(4) 线程锁的简单使用

    要用到多线程 ,就不得不考虑,线程之间的交互,线程是否安全 推荐一个原文链接 是关于 线程锁的基本使用的  http://blog.csdn.net/qq_30513483/article/detai ...

  3. iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue

    多线程队列是装载线程任务的队形结构.(系统以先进先出的方式调度队列中的任务执行 FIFO).在GCD中有两种队列: 串行队列.并发队列. 队列 :串行队列.并发队列,全局主对列,全局并发队列 2.1. ...

  4. ios多线程开发的常用三种方式

    1.NSThread 2.NSOperationQueue 3.GCD NSThread: 创建方式主要有两种: [NSThread detachNewThreadSelector:@selector ...

  5. C#多线程的简单理解

    一.CLR线程池基础 创建和销毁线程是一个昂贵的操作,所以CLR管理了一个线程池(thread pool),可以将线程池看成一个黑盒. CLR初始化时,线程池中是没有线程的.线程的初始化与其他线程一样 ...

  6. ios -RunLoop(简单理解)

    一. RunLoop简介 RunLoop字面意思是运行时,即跑圈得意思.它可以在我们需要的时候自己跑起来运行,在我们没有操作的时候就停下来休息,充分节省CPU资源,提高程序性能. 二. RunLoop ...

  7. iOS NSRunloop的简单理解

    最近学习了下NSRunloop. 作一下简单的理解: 1.runloop与线程的关系,每一个线程创建是都会有伴有一个runloop诞生,runloop用来接收事件源,让线程执行事件.当没有事件处理时, ...

  8. iOS On-Demand Resources简单理解

    ios9引入了一个新功能,On-Demand Resources,它是app thinning 的一部分.这个机能简单的说,就是在下载app的时候,app中包含的不重要资源不下载,等到需要时,在由系统 ...

  9. iOS 块的简单理解

    占位 自己主动转载器那小子,你转完了没? 转完了,我开写了哈! Block,就两个事儿,一个是引用,一个是实例,除了实现处.其他地方都是引用. 以此思路.再继续看看引用和实现的定义方式吧. 參考官方文 ...

随机推荐

  1. mysql 8.0.17 安装配置方法图文教程

    1.URL:https://www.jb51.net/article/167782.htm 2.装好之后需要使用add user中的用户名和密码登录(之前安装数据库时出现的) 使用navicat连接时 ...

  2. webuploader+php如何实现分片+断点续传

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  3. C int类型的数组在内存中的地址示例

    #include <stdio.h> int main(void){ int age[5] = {5,6,7,20,99}; return 0; } //转换后 /*(gdb) p &am ...

  4. phpMyadmin各个版本漏洞【转载】

    原作者:热爱网络安全的小菜狗 原文链接:phpMyadmin各版本漏洞 0x01 PREGREPLACEEVAL漏洞 影响版本:3.5.x < 3.5.8.1 and 4.0.0 < 4. ...

  5. java非空判断

    是否为 null 是否为 "" 是否为空字符串(引号中间有空格)  如: "     ". 制表符.换行符.换页符和回车 一. 字符串 1. if(str == ...

  6. STP生成树详解图

  7. jQuery学习笔记——基本了解

    安装 两种方法: 从 jquery.com 下载jQuery. 从 CDN 中载入 jQuery, 如从我的博客中加载 jQuery: <script src="https://blo ...

  8. ES6——class类继承(读书笔记)

    前言 我一定是一个傻子,昨天这篇文章其实我已经写好了一半了,但是我没有保存 这是学习ES6的过程,我没有系统的看完阮大大的书.零零散散的,很多功能知道,但是没有实际的用过 看了几遍,总是看前面几章,所 ...

  9. 生产者消费者模型Java实现

    生产者消费者模型 生产者消费者模型可以描述为: ①生产者持续生产,直到仓库放满产品,则停止生产进入等待状态:仓库不满后继续生产: ②消费者持续消费,直到仓库空,则停止消费进入等待状态:仓库不空后,继续 ...

  10. vue element-UI 多个 select 回显成功,但是选中无反应

    参考文章 vue开发(四)element的select下拉框设定初值后,不能重新选择的问题 参考文章 element模态框dialog中的select组件中选中无反应 原因 下拉框数据是循环别的接口得 ...