iOS GCD不同场景的使用比較
/**
* async --
并发队列
* 会创建线程。一般同一时候开多条
* 并发运行任务
*/
<span style="font-size:14px;">- (void)asyncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 加入 全局队列 中去 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
</span>
<span style="font-size:14px;">-----下载图片4---<NSThread: 0x7fbd52c9b390>{number = 5, name = (null)}
-----下载图片3---<NSThread: 0x7fbd52f00230>{number = 4, name = (null)}
-----下载图片2---<NSThread: 0x7fbd52e3eed0>{number = 3, name = (null)}
-----下载图片5---<NSThread: 0x7fbd52e2eb40>{number = 6, name = (null)}
-----下载图片1---<NSThread: 0x7fbd52c06010>{number = 2, name = (null)}</span>
/**
* async --
串行队列
* 会创建线程, 一般仅仅开1条线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
*/
- (void)asyncSerialQueue
{
// 1.创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 2.将任务加入到串行队列中 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
}); }
-----下载图片1---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片2---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片3---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片4---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片5---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
/**
* async --
主队列
*/
- (void)asyncMainQueue
{
// 1.主队列(加入到主队列中的任务。都会自己主动放到主线程中去运行)
dispatch_queue_t queue = dispatch_get_main_queue(); // 2.加入 任务 到主队列中 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7faaca905000>{number = 1, name = main}
/**
* sync --
主队列(会卡死)
*/
- (void)syncMainQueue
{
NSLog(@"syncMainQueue----begin--"); // 1.主队列(加入到主队列中的任务,都会自己主动放到主线程中去运行)
dispatch_queue_t queue = dispatch_get_main_queue(); // 2.加入 任务 到主队列中 异步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
}); NSLog(@"syncMainQueue----end--");
}
syncMainQueue----begin--
/**
* sync --
并发队列
* 不会创建线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
* 并发队列失去了并发的功能
*/
- (void)syncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 将 任务 加入到 全局并发队列 中 同步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
/**
* sync --
串行队列
* 不会创建线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
*/
- (void)syncSerialQueue
{
// 创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL); // 将 任务 加入到 串行队列 中 同步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
iOS GCD不同场景的使用比較的更多相关文章
- iOS 开发之 GCD 不同场景使用
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- iOS GCD之dispatch_semaphore(信号量)
前言 最近在看AFNetworking3.0源码时,注意到在 AFURLSessionManager.m 里面的 tasksForKeyPath: 方法 (L681),dispatch_semapho ...
- iOS GCD基础篇 - 同步、异步,并发、并行的理解
1.关于GCD - GCD全称是Grand Central Dispatch - GCD是苹果公司为多核的并行运算提出的解决方案 - GCD会自动利用更多的CPU内核(比如双核.四核) - GC ...
- IOS GCD 使用 (二)
上一节,主要介绍了GCD的基本的概念,这节将用代码深入详细介绍GCD的使用. 一 使用介绍 GCD的使用主要分为三步:创建代码块;选择或创建合适的分发队列;(同步.异步方式)向分发队列提交任 ...
- iOS GCD 编程小结
一.简单介绍 1.GCD简介? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD优势 GCD是苹果公司为多核的并行运算提出的 ...
- iOS GCD NSOperation NSThread等多线程各种举例详解(拷贝)
2年多的iOS之路匆匆而过,期间也拜读来不少大神的博客,近来突然为自己一直做伸手党感到羞耻,是时候回馈社会.回想当年自己还是小白的时候,照着一些iOS多线程教程学,也只是照抄,只知其然.不知其所以然. ...
- iOS GCD中的dispatch_group
假如有一组任务,A,B,C,D,其中ABC是可以并行的,D是必须在ABC任务完成后再执行的. (举个场景,比如吃饭前必须先做菜.做饭和买饮料,然后才能开吃) 1.关于ABC的并行: 采用多线程的方式就 ...
- ios - GCD简单小结
首先GCD两个名词: 队列 同步异步. 队列: 任务放到队列,队列中的任务执行方式取决于执行队列中任务的方式---同步异步. 串行队列: 任务顺序执行,可以叫阻塞队列.只有前面任务完成才执行后面的. ...
- iOS GCD 与 NSOperationQueue
NSOperationQueue ios NSOperation vs. GCD StackOverflow: NSOperation vs. Grand Central Dispatch Blog: ...
随机推荐
- Python_Tips[5] -> 可变数据类型作为初始化形参
可变数据类型作为初始化形参 / Mutable Parameter as Init Formal-para 由于在Python中,没有类似C语言的static静态参数,因此当一个函数需要一个只初始化一 ...
- CSS 标准发布流程
随着 CSS 3 的广泛应用,很多新的 CSS 属性层出不穷,有很多陌生的 CSS 属性出现,所以经常需要去学习新的 CSS 属性.新的属性往往介绍文章不多,所以有时候就需要去看看官方文档,此时会发现 ...
- Binary Tree Maximum Path Sum - LeetCode
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 无需重新编译php加入ftp扩展的解决方法
无需重新编译php加入ftp扩展的解决方法 本文为大家介绍无需重新编译php加入ftp扩展的方法,有需要的朋友可以参考下 首先,进入源码目录cd php-5.2.13/ext/ftp #运行p ...
- adb devices 找不到设备怎么办 --- 2
问题现象:在电脑上安装好手机驱动后,手机进入设置---->应用程序---->开发----->勾选USB调试后连接电脑,,在CMD命令中输入adb devices发现没有设备. 解决方 ...
- DoTween 部分中文文档
前言 DOTween现在还处于 alpha,所以还有一些缺失的功能(如路径插件,附加回调和其它的tween选项),这个文档在不久的将来可能会改变. 一.术语 Tweener 一个tween控制valu ...
- 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';
后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...
- 设计模式之中介者模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性. ...
- 获得Oracle当前日期的年或月的第一天和最后一天
.当前日期的年份第一天和最后一天 第一天 select trunc(sysdate,'y') FROM DUAL; select trunc(sysdate,'yy') FROM DUAL; sele ...
- Xml解析(Dom解析xml)
xml四种解析方式: DOM 平台无关的官方解析方式 优点:形成了树结构,直观好理解,代码更易编写 解析过程中树结构保留在内存中,方便修改 缺点:当xml文件较大时,对内存耗费比较大,容易影响解析性能 ...