多线程队列是装载线程任务的队形结构。(系统以先进先出的方式调度队列中的任务执行 FIFO)。在GCD中有两种队列:

串行队列、并发队列。

队列 :串行队列、并发队列,全局主对列,全局并发队列

2.1.  串行队列:线程只能依次有序的执行。

2.1.1 串行方法 1

- (void)SerialQueueOne{

    NSLog(@"串行1 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);

    dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"串行1 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"串行1 index %d ::: %@",i,[NSThread currentThread]);
}
});
NSLog(@"串行1 end :::%@",[NSThread currentThread]);
}

执行结果:::

2017-12-20 13:49:47.427330+0800 DeadThread[8972:2450330] 串行1 start :::<NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427470+0800 DeadThread[8972:2450330] 串行1 index 0 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427567+0800 DeadThread[8972:2450330] 串行1 index 1 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427636+0800 DeadThread[8972:2450330] 串行1 index 2 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427696+0800 DeadThread[8972:2450330] 串行1 index 10 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427819+0800 DeadThread[8972:2450330] 串行1 index 11 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427893+0800 DeadThread[8972:2450330] 串行1 index 12 ::: <NSThread: 0x60800006ae80>{number = 1, name = main}
2017-12-20 13:49:47.427966+0800 DeadThread[8972:2450330] 串行1 end :::<NSThread: 0x60800006ae80>{number = 1, name = main}

得到结果:::

1.代码顺序 执行;

2.1.2 串行方法 2

- (void)SerialQueueTwo{

    NSLog(@"串行2 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);

   dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"串行2 index %d ::: %@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"串行2 index %d ::: %@",i,[NSThread currentThread]);
}
}); NSLog(@"串行2 end :::%@",[NSThread currentThread]);
}

执行结果:::

2017-12-20 13:50:47.130380+0800 DeadThread[8993:2458517] 串行1 start :::<NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.130533+0800 DeadThread[8993:2458517] 串行1 index 0 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.130648+0800 DeadThread[8993:2458517] 串行1 index 1 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.130724+0800 DeadThread[8993:2458517] 串行1 index 2 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.130896+0800 DeadThread[8993:2458517] 串行1 index 10 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.130979+0800 DeadThread[8993:2458517] 串行1 index 11 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.131057+0800 DeadThread[8993:2458517] 串行1 index 12 ::: <NSThread: 0x60c000071300>{number = 1, name = main}
2017-12-20 13:50:47.131130+0800 DeadThread[8993:2458517] 串行1 end :::<NSThread: 0x60c000071300>{number = 1, name = main}

得到结果:::

1.没有开启线程

2.代码顺序执行;

2.2 并发队列:线程可以同时一起进行执行。实际上是CPU在多条线程之间快速的切换。(并发功能只有在异步(dispatch_async)函数下才有效)

2.2.1 并发方法 1

- (void)concurrentQueueOne{

    NSLog(@"并发1 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"并发1 index %d ::: %@",i,[NSThread currentThread]);
}
}); dispatch_sync(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"并发1 index %d ::: %@",i,[NSThread currentThread]);
}
}); NSLog(@"并发1 end :::%@",[NSThread currentThread]);
}

执行结果:::

2017-12-20 13:52:37.606997+0800 DeadThread[9023:2470506] 并发1 start :::<NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.607130+0800 DeadThread[9023:2470506] 并发1 index 0 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.607197+0800 DeadThread[9023:2470506] 并发1 index 1 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.607447+0800 DeadThread[9023:2470506] 并发1 index 2 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.607685+0800 DeadThread[9023:2470506] 并发1 index 10 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.607891+0800 DeadThread[9023:2470506] 并发1 index 11 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.608056+0800 DeadThread[9023:2470506] 并发1 index 12 ::: <NSThread: 0x608000261600>{number = 1, name = main}
2017-12-20 13:52:37.608190+0800 DeadThread[9023:2470506] 并发1 end :::<NSThread: 0x608000261600>{number = 1, name = main}

得到结果:::

1.线程顺序执行

2.2.2 并发方法 2

- (void)concurrentQueueTwo{

    NSLog(@"并发2 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"并发2 index %d ::: %@",i,[NSThread currentThread]);
}
}); dispatch_async(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"并发2 index %d ::: %@",i,[NSThread currentThread]);
}
}); NSLog(@"并发2 end :::%@",[NSThread currentThread]);
}

执行结果::;

2017-12-20 13:56:45.573695+0800 DeadThread[9084:2492640] 并发2 start :::<NSThread: 0x604000077d80>{number = 1, name = main}
2017-12-20 13:56:45.573891+0800 DeadThread[9084:2492640] 并发2 end :::<NSThread: 0x604000077d80>{number = 1, name = main}
2017-12-20 13:56:45.573907+0800 DeadThread[9084:2492674] 并发2 index 0 ::: <NSThread: 0x60c000265a40>{number = 3, name = (null)}
2017-12-20 13:56:45.573908+0800 DeadThread[9084:2492675] 并发2 index 10 ::: <NSThread: 0x60400026f000>{number = 4, name = (null)}
2017-12-20 13:56:45.574283+0800 DeadThread[9084:2492674] 并发2 index 1 ::: <NSThread: 0x60c000265a40>{number = 3, name = (null)}
2017-12-20 13:56:45.574344+0800 DeadThread[9084:2492675] 并发2 index 11 ::: <NSThread: 0x60400026f000>{number = 4, name = (null)}
2017-12-20 13:56:45.574420+0800 DeadThread[9084:2492675] 并发2 index 12 ::: <NSThread: 0x60400026f000>{number = 4, name = (null)}
2017-12-20 13:56:45.574422+0800 DeadThread[9084:2492674] 并发2 index 2 ::: <NSThread: 0x60c000265a40>{number = 3, name = (null)}

得到结果:::

1.添加两个 任务代码块,开启两个线程;

2.子线程中代码 不是按顺序执行

2.3 全局主队列::::

2.3.1  主队列 同步 死锁

- (void)syncMain {

    NSLog(@"\n\n**************主队列同步,放到主线程会死锁***************\n\n");

    // 主队列
dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列同步1 %@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列同步2 %@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列同步3 %@",[NSThread currentThread]);
}
});
}

死锁原因:::

    如果在主线程中运用主队列同步,也就是把任务放到了主线程的队列中。

    而同步对于任务是立刻执行的,那么当把第一个任务放进主队列时,它就会立马执行。

    可是主线程现在正在处理syncMain方法,任务需要等syncMain执行完才能执行。

    syncMain执行到第一个任务的时候,又要等第一个任务执行完才能往下执行第二个和第三个任务。

    这样syncMain方法和第一个任务就开始了互相等待,形成了死锁。

2.3.2  主队列 异步

- (void)asyncMain {

    NSLog(@"**************主队列异步***************");

    // 主队列
dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列异步1 %@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列异步2 %@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"主队列异步3 %@",[NSThread currentThread]);
}
});
}

执行结果:::

2017-12-20 14:20:01.729412+0800 DeadThread[9257:2636939] **************主队列异步***************
2017-12-20 14:20:01.732208+0800 DeadThread[9257:2636939] 主队列异步1 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.732326+0800 DeadThread[9257:2636939] 主队列异步1 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.732456+0800 DeadThread[9257:2636939] 主队列异步1 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.732726+0800 DeadThread[9257:2636939] 主队列异步2 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.732931+0800 DeadThread[9257:2636939] 主队列异步2 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.733026+0800 DeadThread[9257:2636939] 主队列异步2 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.733128+0800 DeadThread[9257:2636939] 主队列异步3 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.733251+0800 DeadThread[9257:2636939] 主队列异步3 <NSThread: 0x604000261080>{number = 1, name = main}
2017-12-20 14:20:01.733502+0800 DeadThread[9257:2636939] 主队列异步3 <NSThread: 0x604000261080>{number = 1, name = main}

得到结果:::

1. 主队列是个同步队列

2.4 全局并发队列

2.4.1

- (void)globalQueueOne{

    NSLog(@"global1 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);

    dispatch_sync(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"global1 index %d ::: %@",i,[NSThread currentThread]);
}
}); dispatch_sync(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"global1 index %d ::: %@",i,[NSThread currentThread]);
}
}); NSLog(@"global1 end :::%@",[NSThread currentThread]);
}

执行结果:::

2017-12-20 14:27:02.302953+0800 DeadThread[9352:2669397] global1 start :::<NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303132+0800 DeadThread[9352:2669397] global1 index 0 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303230+0800 DeadThread[9352:2669397] global1 index 1 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303322+0800 DeadThread[9352:2669397] global1 index 2 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303397+0800 DeadThread[9352:2669397] global1 index 10 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303467+0800 DeadThread[9352:2669397] global1 index 11 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303557+0800 DeadThread[9352:2669397] global1 index 12 ::: <NSThread: 0x6000000655c0>{number = 1, name = main}
2017-12-20 14:27:02.303638+0800 DeadThread[9352:2669397] global1 end :::<NSThread: 0x6000000655c0>{number = 1, name = main}

2.4.2

- (void)globalQueueTwo{

    NSLog(@"global2 start :::%@",[NSThread currentThread]);

    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
for (int i = 0; i < 3; i++) {
NSLog(@"global2 index %d ::: %@",i,[NSThread currentThread]);
}
}); dispatch_async(queue, ^{
for (int i = 10; i < 13; i++) {
NSLog(@"global2 index %d ::: %@",i,[NSThread currentThread]);
}
}); NSLog(@"global2 end :::%@",[NSThread currentThread]);
}

执行结果:::

2017-12-20 14:28:27.498062+0800 DeadThread[9382:2678820] global2 start :::<NSThread: 0x600000076700>{number = 1, name = main}
2017-12-20 14:28:27.498208+0800 DeadThread[9382:2678820] global2 end :::<NSThread: 0x600000076700>{number = 1, name = main}
2017-12-20 14:28:27.498250+0800 DeadThread[9382:2679707] global2 index 0 ::: <NSThread: 0x60c0000779c0>{number = 3, name = (null)}
2017-12-20 14:28:27.498260+0800 DeadThread[9382:2679706] global2 index 10 ::: <NSThread: 0x60400007e800>{number = 4, name = (null)}
2017-12-20 14:28:27.498555+0800 DeadThread[9382:2679707] global2 index 1 ::: <NSThread: 0x60c0000779c0>{number = 3, name = (null)}
2017-12-20 14:28:27.498692+0800 DeadThread[9382:2679706] global2 index 11 ::: <NSThread: 0x60400007e800>{number = 4, name = (null)}
2017-12-20 14:28:27.498710+0800 DeadThread[9382:2679707] global2 index 2 ::: <NSThread: 0x60c0000779c0>{number = 3, name = (null)}
2017-12-20 14:28:27.498753+0800 DeadThread[9382:2679706] global2 index 12 ::: <NSThread: 0x60400007e800>{number = 4, name = (null)}

iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue的更多相关文章

  1. ios多线程操作(五)—— GCD串行队列与并发队列

          GCD的队列能够分为2大类型,分别为串行队列和并发队列      串行队列(Serial Dispatch Queue):      一次仅仅调度一个任务,队列中的任务一个接着一个地运行( ...

  2. iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合

    通过对前面两偏线程理解的总结,自己对线程的理解也逐渐加深,梳理的清晰起来…… 通常在使用线程 的时候,都是要用到 执行对列,执行方式,执行任务, 现在开始新一轮的深入 3. 1. 1  同步 + 串行 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Linux运维:安装CentOS7图解

    Ago linux运维群: 93324526 笔者QQ:578843228 此篇博文针对最小化安装,和只有图解.有不懂地方,欢迎加群询问. 此篇以CentOS7.2为例

  2. Halting Problem

    Halting Problem: 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4049 总结一个小规律:题目中给的 ...

  3. 通过 frp 实现群晖的 drive 远端同步

    通过 frp 实现 drive 同步 其实其他的也类似, 只是指定 tcp 的端口不一致就可以. frp 实现的外网端口映射"肥肠"方便. 也推广下 frp 的地址 frp-git ...

  4. vs code 修改选中后匹配的代码的颜色

    打开设置文件 输入 { "workbench.colorCustomizations": { "editor.selectionBackground": &qu ...

  5. 模板 - 数学 - 数论 - 扩展Euler定理

    费马(Fermat)小定理 当 \(p\) 为质数,则 \(a^{p-1}\equiv 1 \mod p\) 反之,费马小定理的逆定理不成立,这样的数叫做伪质数,最小的伪质数是341. 欧拉(Eule ...

  6. Codeforces Round #607 (Div. 2)

    A - Suffix Three 题意:规定三种语言的结尾符,给出字符串,判断是什么语言. void test_case() { string s; cin >> s; reverse(s ...

  7. js off 缓动动画

    动画也有很多种,一起来学习,缓动动画吧 缓动动画 1.缓动动画原理=盒子位置+(目标盒子位置-现在盒子位置)/10 2.步长越来越小 3.让步长越来越小的公式      步长=(目标位置-本身位置)/ ...

  8. java 面试题目(java高级架构)

    题目信息 java基础: 1. Java 基础 JDK 和 JRE 有什么区别?   Java中JDK和JRE的区别是什么?它们的作用分别是什么? == 和 equals 的区别是什么? 两个对象的 ...

  9. IdHTTPServer开发https服务器

    IdHTTPServer开发https服务器 该篇经验同样适用于DATASNAP和UNIGUI,因为它们都基于INDY10. 1)需要TIdServerIOHandlerSSLOpenSSL控件 2) ...

  10. cv2.fillConvexPoly()与cv2.fillPoly()填充多边形

    cv2.fillConvexPoly() cv2.fillConvexPoly()函数可以用来填充凸多边形,只需要提供凸多边形的顶点即可. 我们来画一个三角形 img = np.zeros((1080 ...