GCD的使用方法
来源:互联网 发布:php中介者模式 编辑:程序博客网 时间:2024/06/15 15:56
在项目开发中 我们用到最多的就是GCD
特总结常用的几点与大家分享
#import "ViewController.h"@interface ViewController (){ /** * 串行队列 */ dispatch_queue_t _mainQueue; /** * 并行队列 */ dispatch_queue_t _globelQueue;}@end@implementation ViewController/* 异步调度和同步调度 异步调度 dispatch_async : 把一个任务添加到某queue后就马上离开,而不管任务在那个queue里的执行状态 同步调度 dispatch_sync : 把一个任务添加到某queue后,等这个任务完成,调用线程才继续执行. 所以,异步调度和同步调度的区别不在于被添加的任务怎样执行,而在于调用线程是否等待任务执行完 */- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 串行队列// 并行队列 //GCD:Grand-Central-Dispatch; #if 0 /* 死锁 */ NSLog(@"1111111111"); dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"222222222222"); }); NSLog(@"33333333333"); 只会打印11111111111#endif _mainQueue = dispatch_get_main_queue(); _globelQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); [self test4]; #if 0 公式: dispatch_async(_globelQueue, ^{ //请求数据 dispatch_async(_mainQueue, ^{ //刷新UI }); });#endif }//A,B,C表示3个任务,A,B,C并行执行- (void)test1{ //A dispatch_async(_globelQueue, ^{ [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"A"]; }); //B dispatch_async(_globelQueue, ^{ [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"B"]; }); //C dispatch_async(_globelQueue, ^{ [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"C"]; });}//A,B并发执行,最后C- (void)test2{ //调度组 dispatch_group_t group = dispatch_group_create(); //A dispatch_group_async(group, _globelQueue, ^{ [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"A"]; }); //B dispatch_group_async(group, _globelQueue, ^{ [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"B"]; }); //1.其他任务都执行完,最后刷新UI //执行的任务 C dispatch_group_notify(group, _mainQueue, ^{ //刷新 }); //2.有一个下载需要最后执行 dispatch_group_async(group, _globelQueue, ^{ //执行最后一个任务 //my code dispatch_async(_mainQueue, ^{ //刷新UI }); });}//A,B并发执行,然后执行C,最后D,E(一个任务把其他任务分开)- (void)test3{ //barrier需要自定义队列 /* dispatch_queue_create("队列名字", 串行/并行) NULL表示DISPATCH_QUEUE_SERIAL DISPATCH_QUEUE_SERIAL 串行 DISPATCH_QUEUE_CONCURRENT 并行 */ NSString *urlString = @"http://g.hiphotos.baidu.com/image/h%3D768%3Bcrop%3D0%2C0%2C1024%2C768/sign=ad148bd817ce36d3bd04813602c859f5/42166d224f4a20a4b0824bc592529822720ed010.jpg"; dispatch_queue_t customQueue = dispatch_queue_create("com.xxxx.queue", DISPATCH_QUEUE_CONCURRENT); //A dispatch_async(customQueue, ^{ [self downloadWithUrlString:urlString string:@"A"]; }); //B dispatch_async(customQueue, ^{ [self downloadWithUrlString:urlString string:@"B"]; }); //C dispatch_barrier_async(customQueue, ^{ [self downloadWithUrlString:urlString string:@"C"]; }); //D dispatch_async(customQueue, ^{ [self downloadWithUrlString:urlString string:@"D"]; }); //E dispatch_async(customQueue, ^{ [self downloadWithUrlString:urlString string:@"E"]; });}//执行某个任务多少次- (void)test4{ dispatch_apply(5, _globelQueue, ^(size_t t) { NSLog(@"%@",[NSString stringWithFormat:@"%zu",t]); });}//只执行一次,一般用在单例- (void)test5{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ });}//延迟调用- (void)test6{ /* ull 表示微秒 */ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ });}- (void)downloadWithUrlString:(NSString *)urlString string:(NSString *)string{ NSLog(@"开始下载 -- %@",string); NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; sleep(2); NSLog(@"下载完成 -- %@",string);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- GCD的使用方法
- GCD--dispatch 的使用方法介绍
- iOS中GCD的使用方法
- GCD 使用方法
- GCD(Grand Central Dispatch)的使用方法
- iOS—GCD使用方法
- GCD常用使用方法
- 【读书笔记】iOS-GCD-使用方法
- IOS-12-关于GCD多线程:dispatch_async 与 dispatch_get_global_queue 的使用方法
- GCD 同步 异步 串行 并发的使用方法.例子 和特点
- GCD 同步 异步 串行 并发的使用方法.例子 和特点
- GCD 详细介绍及使用方法
- GCD使用方法以及死锁研究
- GCD深入学习之GCD的初识
- 高端GCD 关于GCD的拓展
- gcd的递归表现形式
- [bzoj2820] yy的gcd
- 简单的gcd函数
- window下编译QT5出现错误:Cannot find file: D:\用户目录\Documents\ThreadClass\ThreadClass.pro.
- kafka命令记录
- 友盟数据统计、消息推送、社会化分享
- POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
- hyperic hq安装
- GCD的使用方法
- UVA639八皇后变形式之中国象棋盘上放車
- mysql添加远程访问
- HttpHandler与HttpModule的用处与区别
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- Android事件的传递机制
- jz2440下的input输入子系统
- 消费者和生产者
- OpenCV库的使用