iOS开发中的并发、串行队列,同步、异步任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/69914369

在多线程开发中我们经常会遇到这些概念:并发队列、串行队列、同步任务、异步任务。我们将这四个概念进行组合会有四种结果:串行队列+同步任务、串行队列+异步任务、并发队列+同步任务、并发队列+异步任务。我们对这四种结果进行解释:

1.串行队列+同步任务:不会开启新的线程,任务逐步完成。

2.串行队列+异步任务:开启新的线程,任务逐步完成。

3.并发队列+同步任务:不会开启新的线程,任务逐步完成。

4.并发队列+异步任务:开启新的线程,任务同步完成。

我们如果要让任务在新的线程中完成,应该使用异步线程。为了提高效率,我们还应该将任务放在并发队列中。因此在开发中使用最多的是并发队列+异步任务。看代码:

// 串行队列+同步任务

- (void)serialSyn{

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

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 串行队列+异步任务

- (void)serialAsyn{

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

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 并发队列+同步任务

- (void)concurrenSyn{

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

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_sync(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 并发队列+异步任务

- (void)concurrentAsyn{

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

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

dispatch_async(queue, ^{

for (int i =0; i <3; i ++) {

NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

输出结果分别为:

// 串行队列+同步任务

2017-12-09 15:50:18.427989+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:20.912799+0800 GCD[1044:27277] XPC connection interrupted

2017-12-09 15:50:21.429204+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:24.430559+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:25.431937+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:26.433460+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:27.433913+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:28.435443+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:29.435987+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:30.437512+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}

// 串行队列+异步任务

2017-12-09 16:08:03.242688+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:06.246989+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:09.249206+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:10.250193+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:11.250878+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:12.255908+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:13.258215+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:14.258708+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:15.259192+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

// 串行队列+异步任务

2017-12-09 16:09:23.548551+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:25.547003+0800 GCD[1283:43153] XPC connection interrupted

2017-12-09 16:09:26.549830+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:29.550406+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:30.551134+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:31.552655+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:32.553861+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:33.555326+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:34.555842+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:35.556267+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}

// 并发队列+异步任务

2017-12-09 16:10:26.676320+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:26.676320+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:27.678310+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:27.678316+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:28.674756+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}

2017-12-09 16:10:28.679805+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:28.679798+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:31.675355+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}

2017-12-09 16:10:34.678046+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}

看表格:

注意:

在主队列中添加同步任务会产生死锁,进而导致程序崩溃。

代码:

- (void)viewDidLoad {

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

NSLog(@"===========1");

dispatch_sync(dispatch_get_main_queue(), ^{

NSLog(@"===========2");

    });

NSLog(@"===========3");

}

以上代码在打印出1之后就卡死在了dispatch_sync...的代码上。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏别先生

一脸懵逼学习Hive的安装(将sql语句翻译成MapReduce程序的一个工具)

Hive只在一个节点上安装即可: 1.上传tar包:这个上传就不贴图了,贴一下上传后的,看一下虚拟机吧: ? 2.解压操作: [root@slaver3...

2315
来自专栏Janti

springboot之定时任务

说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务。 回顾一下定时线程池。

752
来自专栏谈补锅

拖拽手势和清扫手势冲突时(UIPanGestureRecognizer和UISwipeGestureRecognizer冲突时)

  故事发生在这样的情境上:给整个控制器添加了一个拖拽手势,然后又在控制上的每个Cell上加了左滑清扫手势,然后问题来了:只有拖拽手势起作用,而左滑手势没有效果...

2022
来自专栏影子

SpringBoot自定义拦截器实现IP白名单功能

转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html

8921
来自专栏Spark学习技巧

Hbase源码系列之regionserver应答数据请求服务设计

一,基本介绍 Hbase源码系列主要是以hbase-1.0.0为例讲解hbase源码。本文主要是将Regionserver服务端RPC的结构及处理流程。希望是帮...

6099
来自专栏一“技”之长

iOS中CoreData数据管理系列四——进行数据与页面的绑定

    在上一篇博客中,我们讨论了CoreData框架中添加与查询数据的操作,事实上,在大多数情况下,这些数据都是由一个UITableView表视图进行展示的,...

751
来自专栏影子

SpringBoot自定义拦截器实现IP白名单功能

1.5K11
来自专栏JackieZheng

Spring实战——Profile

  看到Profile这个关键字,或许你从来没有正眼瞧过他,又或者脑海中有些模糊的印象,比如除了这里Springmvc中的Profile,maven中也有Pro...

3096
来自专栏影子

SpringBoot自定义拦截器实现IP白名单功能

4K2
来自专栏积累沉淀

Quartz学习

Quartz学习 介绍Quartz Quartz是一个开源的任务调度系统,它能用来调度很多任务的执行。 运行环境 Quartz 能嵌入在其他应用程序里运行。 Q...

37710

扫码关注云+社区

领取腾讯云代金券