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

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

现在开始新一轮的深入

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. 关于Keil4 转到 Keil5以后的一些错误解决

    一, 看自己选择CPU型号,根据型号再做配置 根据自己型号填写

  2. Processing 2.1.1 无法使用video和movie问题解决方案

    升级到2.1.1后,尝试着运行了一下sample中的video和movie,结果都报错,上网搜了一下,找到解决方法. 1. 首先来看movie的错误,如下: JNA: Callback org.gst ...

  3. 【模板】最小割树(Gomory-Hu Tree)

    传送门 Description 给定一个\(n\)个点\(m\)条边的无向连通图,多次询问两点之间的最小割 两点间的最小割是这样定义的:原图的每条边有一个割断它的代价,你需要用最小的代价使得这两个点不 ...

  4. Vue 一个组件引用另一个组件

    有些时候需要这么做,比如,我想在首页加载轮播组件,但是又不想全局注册(因为不是每个页面都需要轮播功能) 方法1: <template> <div> <!-- 3.在tem ...

  5. 用sublime3编写运行16位汇编程序_详细教程

    最近需要学8086汇编,课堂教学竟然是PPT看代码,然而不运行程序是没法学编程的.网上的教程有很多坑点,摸索出了正确的步骤. 1.安装sublime3.安装MASM32.64位系统安装DOSBOX(因 ...

  6. 14.LAMP服务 Linux Apache Mysql Php和防护机制 xinetd、tcp wapper

    一.安装LAMP服务 Linux Apache Mysql Php       要求操作系统支持 php解析 apache调用php插件解析 phpmyadmin       yum install ...

  7. linux内核是在哪里创建1号进程的?

    1. 请看rest_init的完整代码(不看也没关系,内核版本为5.2, init/main.c) noinline void __ref rest_init(void) { struct task_ ...

  8. npm WARN deprecated fsevents windows

    更新下 使用yarn貌似会帮助跳过这个问题: info fsevents@2.1.2: The platform "win32" is incompatible with this ...

  9. 服务器端实时推送技术之SseEmitter的用法

    这是SpringMVC提供的一种技术,可以实现服务端向客户端实时推送数据.用法非常简单,只需要在Controller提供一个接口,创建并返回SseEmitter对象,发送数据可以在另一个接口调用其se ...

  10. 阿里重磅开源在线分析诊断工具Arthas(阿尔萨斯)

    github地址: Arthas English version goes here. Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Art ...