参考文章:

http://www.cnblogs.com/mddblog/p/4767559.html

重要结论:

  1)串行队列,同步执行-----串行队列意味着顺序执行,同步执行意味着不开启线程(在当前线程执行)

  2)串行队列,异步执行-----串行队列意味着任务顺序执行,异步执行说明要开线程, (如果开多个线程的话,不能保证串行队列顺序执行,所以只开一个线程)

  3)并行队列,异步执行-----并行队列意味着执行顺序不确定,异步执行意味着会开启线程,而并行队列又允许不按顺序执行,所以系统为了提高性能会开启多个线程,来队列取任务(队列中任务取出仍然是顺序取出的,只是线程执行无序)。

  4)并行队列,同步执行-----同步执行意味着不开线程,则肯定是顺序执行

分别进行验证

串行同步

// 串行同步
- (void)test_queue_serial_sync
{
NSLog(@"串行同步");
dispatch_queue_t queue = dispatch_queue_create("com.demo.001", DISPATCH_QUEUE_SERIAL);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_sync(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
// 结果
2017-02-10 18:00:21.991383 RXVerifyExample[4927:2465789] 串行同步
2017-02-10 18:00:21.991636 RXVerifyExample[4927:2465789] main thread:0x17406cb00
2017-02-10 18:00:21.992020 RXVerifyExample[4927:2465789] 1 thread: 0x17406cb00
2017-02-10 18:00:21.992097 RXVerifyExample[4927:2465789] 2 thread: 0x17406cb00
2017-02-10 18:00:21.992165 RXVerifyExample[4927:2465789] 3 thread: 0x17406cb00
2017-02-10 18:00:21.992230 RXVerifyExample[4927:2465789] 4 thread: 0x17406cb00
 

结论:

串行同步,不创建任何新的线程,因为是在主线程中执行这个方法的,所以,同步方法都在主线程。

并行同步

// 并行同步
- (void)test_queue_concurrent_sync
{
NSLog(@"并行同步");
dispatch_queue_t queue = dispatch_queue_create("com.demo.002", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_sync(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
// 结果:
2017-02-10 18:04:33.107907 RXVerifyExample[4930:2467403] 并行同步
2017-02-10 18:04:33.108053 RXVerifyExample[4930:2467403] main thread:0x174066600
2017-02-10 18:04:33.108121 RXVerifyExample[4930:2467403] 1 thread: 0x174066600
2017-02-10 18:04:33.108177 RXVerifyExample[4930:2467403] 2 thread: 0x174066600
2017-02-10 18:04:33.108219 RXVerifyExample[4930:2467403] 3 thread: 0x174066600
2017-02-10 18:04:33.108259 RXVerifyExample[4930:2467403] 4 thread: 0x174066600
 

结论:跟串行同步是一样的。

串行异步1

// 串行异步1
- (void)test_queue_serial_async1
{
NSLog(@"串行异步方法1");
dispatch_queue_t queue = dispatch_queue_create("com.demo.0031", DISPATCH_QUEUE_SERIAL);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_async(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
// 结果:
-- ::15.124048 RXVerifyExample[:] 串行异步方法1
-- ::15.124244 RXVerifyExample[:] main thread:0x174076a40
-- ::15.125490 RXVerifyExample[:] thread: 0x174271800
-- ::15.125574 RXVerifyExample[:] thread: 0x174271800
-- ::15.125643 RXVerifyExample[:] thread: 0x174271800
-- ::15.125710 RXVerifyExample[:] thread: 0x174271800

串行异步2

// 串行异步2
- (void)test_queue_serial_async2
{
NSLog(@"串行异步方法2");
dispatch_queue_t queue = dispatch_queue_create("com.demo.0032", DISPATCH_QUEUE_SERIAL);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_async(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4.1];
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3.1];
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
// 结果
-- ::16.478508 RXVerifyExample[:] 串行异步方法2
-- ::16.478611 RXVerifyExample[:] main thread:0x170072380
-- ::16.479034 RXVerifyExample[:] thread: 0x174264240
-- ::16.479069 RXVerifyExample[:] thread: 0x174264240
-- ::20.584321 RXVerifyExample[:] thread: 0x174264240
-- ::23.689611 RXVerifyExample[:] thread: 0x174264240

结论:

串行异步只创建一个新的线程

并行异步1

// 并行异步1
- (void)test_queue_concurrent_async1
{
NSLog(@"并行异步方法1");
dispatch_queue_t queue = dispatch_queue_create("com.demo.0041", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_async(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
} // 结果
-- ::25.692681 RXVerifyExample[:] 并行异步方法1
-- ::25.692880 RXVerifyExample[:] main thread:0x174073000
-- ::25.693828 RXVerifyExample[:] thread: 0x174267d00
-- ::25.693916 RXVerifyExample[:] thread: 0x174267d00
-- ::25.693988 RXVerifyExample[:] thread: 0x174267d00
-- ::25.694056 RXVerifyExample[:] thread: 0x174267d00

并行异步2

// 并行异步2
- (void)test_queue_concurrent_async2
{
NSLog(@"并行异步方法2");
dispatch_queue_t queue = dispatch_queue_create("com.demo.0042", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_async(queue, ^{
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4.1];
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3.1];
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
// 结果
-- ::27.262009 RXVerifyExample[:] 并行异步方法2
-- ::27.262213 RXVerifyExample[:] main thread:0x17406e080
-- ::27.263710 RXVerifyExample[:] thread: 0x174262e80
-- ::27.263811 RXVerifyExample[:] thread: 0x174262e80
-- ::30.369209 RXVerifyExample[:] thread: 0x1742648c0
-- ::31.369205 RXVerifyExample[:] thread: 0x174262e80

并行异步3

// 并行异步3
- (void)test_queue_concurrent_async3
{
NSLog(@"并行异步方法3");
dispatch_queue_t queue = dispatch_queue_create("com.demo.0043", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"main thread:%p", [NSThread mainThread]);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2.1];
NSLog(@"1 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1.1];
NSLog(@"2 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4.1];
NSLog(@"3 thread: %p", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3.1];
NSLog(@"4 thread: %p", [NSThread currentThread]);
});
}
//结果
-- ::59.926988 RXVerifyExample[:] 并行异步方法3
-- ::59.927184 RXVerifyExample[:] main thread:0x17007f8c0
-- ::01.033209 RXVerifyExample[:] thread: 0x170462ac0
-- ::02.033478 RXVerifyExample[:] thread: 0x170462f40
-- ::03.037799 RXVerifyExample[:] thread: 0x170462a40
-- ::04.033691 RXVerifyExample[:] thread: 0x170462480

从并行异步的三个例子来看。

创建的新的线程的数目是不确定的,是跟队列中异步方法个数和异步方法执行的时间有关。

iOS gcd 串行,并行,同步,异步代码研究的更多相关文章

  1. IOS多线程知识总结/队列概念/GCD/串行/并行/同步/异步

    进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间: 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程. 队列:dis ...

  2. Python并发编程系列之常用概念剖析:并行 串行 并发 同步 异步 阻塞 非阻塞 进程 线程 协程

    1 引言 并发.并行.串行.同步.异步.阻塞.非阻塞.进程.线程.协程是并发编程中的常见概念,相似却也有却不尽相同,令人头痛,这一篇博文中我们来区分一下这些概念. 2 并发与并行 在解释并发与并行之前 ...

  3. IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)

    进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程 队列 dispa ...

  4. 【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例

    (1)GCD实现的同步异步.串行并行. --同步sync应用场景:用户登录,利用堵塞 --串行异步应用场景:下载等耗时间的任务 /** * 由于是异步.所以开通了子线程.可是由于是串行队列,所以仅仅须 ...

  5. GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例

    转:http://www.tuicool.com/articles/NVVnMn (1)GCD实现的同步异步.串行并行. ——同步sync应用场景:用户登录,利用阻塞 ——串行异步应用场景:下载等耗时 ...

  6. iOS多线程——同步异步串行并行

    串行并行异步同步的概念很容易让人混淆,关于这几个概念我在第一篇GCD中有解释,但是还不够清晰,所以这里重写一篇博客专门对这几个概念进行区分: 先说一下队列和任务: (1)队列分为串行和并行,任务的执行 ...

  7. 串行&并行&并发,同步&异步

    1. 串行&并行&并发 1.1 串行 这个非常好理解,字面意思,像串成一个串一样,顺序执行 上一个没执行完的话,后面的就必须无条件等待 一般情况就是一个线程里:任务一个接一个执行,类似 ...

  8. iOS:GCD理解1(同步-异步、串行-并行)

    1.并行-异步(ST1与ST2抢占资源) 1-1).获取 并行(全局)队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t queu ...

  9. iOS:GCD理解1(串行-并行、同步-异步)

    1.获取并行.创建串行 队列 1-1).获取 并行(全局) 队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t global_qu ...

随机推荐

  1. Nodejs进阶:express+session实现简易身份认证

    本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 文档概览 本文基于express.express-session ...

  2. Android之AIDL知识总结

    1.AIDL介绍 AIDL是一个缩写,全称是Android Interface Definition Language,翻译为Android接口定义语言.主要用于线程之间的通信,本文主要以不同应用之间 ...

  3. C语言精要总结-内存地址对齐与struct大小判断篇

    在笔试时,经常会遇到结构体大小的问题,实际就是在考内存地址对齐.在实际开发中,如果一个结构体会在内存中高频地分配创建,那么掌握内存地址对齐规则,通过简单地自定义对齐方式,或者调整结构体成员的顺序,可以 ...

  4. (转载)Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换 时间:2011-04-08作者:woyoo分类:linux评论:0 我友分享: 新浪微博 腾讯微博 搜狐微博 网易微博 开心网 QQ空间 msn 如果你需要在 ...

  5. git 利用分支概念实现一个仓库管理两个项目

    需求描述:开发了一个网站,上线之际,突然另一个客户说也想要个一样的网站,但网站的logo和内部展示图片需要替换一下,也就是说大部分的后台业务逻辑代码都是一致的,以后升级时功能也要保持一致:刚开始想反正 ...

  6. Linux系统优化

    前言:这篇博客主机讲下安装Linux系统后调优及安全设置 基础环境 一.使用网易163镜像做yum源 默认国外的yum源速度很慢,所以换成国内的. 先备份 下载163yum源:http://mirro ...

  7. 蓝桥杯-平方怪圈-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  8. 蓝桥杯-扑克牌移动-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  9. Windows 10 碎片整理程序使用

    1.打开 此电脑 2.右击任意一个磁盘,这里以C盘为例,点击 属性 3.点击 工具  选项卡--->优化 4.选中其中的一个盘  优化 5.然后静等结束,就好啦. 虽然不知道有什么用,...

  10. 微信小程序框架

    框架 小程序开发框架的目标是通过尽可能简单.高效的方式让开发者可以在微信中开发具有原生 APP 体验的服务. 框架提供了自己的视图层描述语言 WXML 和 WXSS,以及基于 JavaScript 的 ...