标签:with 场景 notify 个人博客 char ash 函数 atom arc
http://www.jianshu.com/p/ae786a4cf3b1
本篇博客共分以下几个模块来介绍GCD的相关内容:
欢迎访问作者个人博客www.dullgrass.com,更多好的文章与您分享
GCD 全称Grand Center Dispatch (抽象层次:高)
缺点: 使用GCD的场景如果很复杂,就有非常大的可能遇到死锁问题。
GCD抽象层次最高,使用也简单,因此,苹果也推荐使用GCD
GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行。
gcd中相关函数的使用一般都是以dispatch开头
dispatch_sync 同步执行任务函数,不会开启新的线程,dispatch_async 异步执行任务函数,会开启新的线程
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue,^{
NSLog("MainQueue");
});
程序一直处于等待状态,block中的代码将执行不到dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue,^{
NSLog("MainQueue");
});
程序正常运行,block中的代码正常运行dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(globalQueue, ^{
//子线程异步执行下载任务,防止主线程卡顿
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSError *error;
NSString *htmlData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (htmlData != nil) {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//异步返回主线程,根据获取的数据,更新UI
dispatch_async(mainQueue, ^{
NSLog(@"根据更新UI界面");
});
} else {
NSLog(@"error when download:%@",error);
}
});
主线程串行队列由系统默认生成的,所以无法调用dispatch_resume()和dispatch_suspend()来控制执行继续或中断。
耗时的操作,比如读取网络数据,IO,数据库读写等,我们会在另外一个线程中处理这些操作,然后通知主线程更新界面
//程序默认的队列级别,一般不要修改,DISPATCH_QUEUE_PRIORITY_DEFAULT == 0
dispatch_queue_t globalQueue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//HIGH
dispatch_queue_t globalQueue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//LOW
dispatch_queue_t globalQueue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
//BACKGROUND
dispatch_queue_t globalQueue4 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"current task");
dispatch_sync(globalQueue, ^{
sleep(2.0);
NSLog(@"sleep 2.0s");
});
NSLog(@"next task");
控制台输出如下:
2015-11-18 15:51:45.550 Whisper[33152:345023] current task
2015-11-18 15:51:47.552 Whisper[33152:345023] sleep 2.0s
2015-11-18 15:51:47.552 Whisper[33152:345023] next task
2s钟之后,才会执行block代码段下面的代码。dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"current task");
dispatch_async(globalQueue, ^{
sleep(2.0);
NSLog(@"sleep 2.0s");
});
NSLog(@"next task");
控制台输出如下:
2015-11-18 15:50:14.999 Whisper[33073:343781] current task
2015-11-18 15:50:15.000 Whisper[33073:343781] next task
2015-11-18 15:50:17.004 Whisper[33073:343841] sleep 2.0s
主线程不用等待2s钟,继续执行block代码段后面的代码。dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"current task");
dispatch_async(globalQueue, ^{
NSLog(@"最先加入全局并发队列");
});
dispatch_async(globalQueue, ^{
NSLog(@"次加入全局并发队列");
});
NSLog(@"next task");
控制台输出如下:
2015-11-18 16:54:52.202 Whisper[39827:403208] current task
2015-11-18 16:54:52.203 Whisper[39827:403208] next task
2015-11-18 16:54:52.205 Whisper[39827:403309] 最先加入全局并发队列
2015-11-18 16:54:52.205 Whisper[39827:403291] 次加入全局并发队列
异步线程的执行顺序是不确定的。几乎同步开始执行自定义串行队列
获取自定义串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("com.dullgrass.serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"%s",dispatch_queue_get_label(conCurrentQueue)) ;
控制台输出:
2015-11-19 11:05:34.469 Whisper[1223:42960] com.dullgrass.serialQueue
dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)函数中第一个参数是给这个queue起的标识,这个在调试的可以看到是哪个队列在执行,或者在crash日志中,也能做为提示。第二个是需要创建的队列类型,是串行的还是并发的
自定义串行队列同步执行任务
dispatch_queue_t serialQueue = dispatch_queue_create("com.dullgrass.serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"current task");
dispatch_sync(serialQueue, ^{
NSLog(@"最先加入自定义串行队列");
sleep(2);
});
dispatch_sync(serialQueue, ^{
NSLog(@"次加入自定义串行队列");
});
NSLog(@"next task");
控制台输出:
2015-11-18 17:09:40.025 Whisper[40241:416296] current task
2015-11-18 17:09:40.027 Whisper[40241:416296] 最先加入自定义串行队列
2015-11-18 17:09:43.027 Whisper[40241:416296] 次加入自定义串行队列
2015-11-18 17:09:43.027 Whisper[40241:416296] next task
当前线程等待串行队列中的子线程执行完成之后再执行,串行队列中先进来的子线程先执行任务,执行完成后,再执行队列中后面的任务。
自定义串行队列嵌套执行同步任务,产生死锁
dispatch_queue_t serialQueue = dispatch_queue_create("com.dullgrass.serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(serialQueue, ^{ //该代码段后面的代码都不会执行,程序被锁定在这里
NSLog(@"会执行的代码");
dispatch_sync(serialQueue, ^{
NSLog(@"代码不执行");
});
});
异步执行串行队列,嵌套同步执行串行队列,同步执行的串行队列中的任务将不会被执行,其他程序正常执行
dispatch_queue_t serialQueue = dispatch_queue_create("com.dullgrass.serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
NSLog(@"会执行的代码");
dispatch_sync(serialQueue, ^{
NSLog(@"代码不执行");
});
});
注意不要嵌套使用同步执行的串行队列任务
自定义并发队列
dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"current task");
dispatch_sync(conCurrentQueue, ^{
NSLog(@"先加入队列");
});
dispatch_sync(conCurrentQueue, ^{
NSLog(@"次加入队列");
});
NSLog(@"next task");
2015-11-19 10:36:23.259 Whisper[827:20596] current task
2015-11-19 10:36:23.261 Whisper[827:20596] 先加入队列
2015-11-19 10:36:23.261 Whisper[827:20596] 次加入队列
2015-11-19 10:36:23.261 Whisper[827:20596] next task
dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"current task");
dispatch_sync(conCurrentQueue, ^{
NSLog(@"先加入队列");
dispatch_sync(conCurrentQueue, ^{
NSLog(@"次加入队列");
});
});
NSLog(@"next task");
2015-11-19 10:39:21.301 Whisper[898:22273] current task
2015-11-19 10:39:21.303 Whisper[898:22273] 先加入队列
2015-11-19 10:39:21.303 Whisper[898:22273] 次加入队列
2015-11-19 10:39:21.303 Whisper[898:22273] next task
dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"current task");
dispatch_async(conCurrentQueue, ^{
NSLog(@"先加入队列");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"次加入队列");
});
NSLog(@"next task");
2015-11-19 10:45:22.290 Whisper[1050:26445] current task
2015-11-19 10:45:22.290 Whisper[1050:26445] next task
2015-11-19 10:45:22.290 Whisper[1050:26505] 次加入队列
2015-11-19 10:45:22.290 Whisper[1050:26500] 先加入队列
异步执行任务,开启新的子线程,不影响当前线程任务的执行,并发队列中的任务,几乎是同步执行的,输出顺序不确定
当遇到需要执行多个线程并发执行,然后等多个线程都结束之后,再汇总执行结果时可以用group queue
使用示例:
dispatch_queue_t conCurrentGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_group_t groupQueue = dispatch_group_create();
NSLog(@"current task");
dispatch_group_async(groupQueue, conCurrentGlobalQueue, ^{
NSLog(@"并行任务1");
});
dispatch_group_async(groupQueue, conCurrentGlobalQueue, ^{
NSLog(@"并行任务2");
});
dispatch_group_notify(groupQueue, mainQueue, ^{
NSLog(@"groupQueue中的任务 都执行完成,回到主线程更新UI");
});
NSLog(@"next task");
控制台输出:
2015-11-19 13:47:55.117 Whisper[1645:97116] current task
2015-11-19 13:47:55.117 Whisper[1645:97116] next task
2015-11-19 13:47:55.119 Whisper[1645:97178] 并行任务1
2015-11-19 13:47:55.119 Whisper[1645:97227] 并行任务2
2015-11-19 13:47:55.171 Whisper[1645:97116] groupQueue中的任务 都执行完成,回到主线程更新UI
在当前线程阻塞的同步等待dispatch_group_wait
dispatch_group_t groupQueue = dispatch_group_create();
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC);
dispatch_queue_t conCurrentGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"current task");
dispatch_group_async(groupQueue, conCurrentGlobalQueue, ^{
long isExecuteOver = dispatch_group_wait(groupQueue, delayTime);
if (isExecuteOver) {
NSLog(@"wait over");
} else {
NSLog(@"not over");
}
NSLog(@"并行任务1");
});
dispatch_group_async(groupQueue, conCurrentGlobalQueue, ^{
NSLog(@"并行任务2");
});
控制台输出如下:
2015-11-19 14:37:29.514 Whisper[2426:126683] current task
2015-11-19 14:37:29.518 Whisper[2426:126791] 并行任务2
2015-11-19 14:37:39.515 Whisper[2426:126733] wait over
2015-11-19 14:37:39.516 Whisper[2426:126733] 并行任务1
dispatch_time(dispatch_time_t when, int64_t delta);
参数注释:
第一个参数一般是DISPATCH_TIME_NOW,表示从现在开始
第二个参数是延时的具体时间
延时1秒可以写成如下几种:
NSEC_PER_SEC----每秒有多少纳秒
dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC);
USEC_PER_SEC----每秒有多少毫秒(注意是指在纳秒的基础上)
dispatch_time(DISPATCH_TIME_NOW, 1000*USEC_PER_SEC); //SEC---毫秒
NSEC_PER_USEC----每毫秒有多少纳秒。
dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC*NSEC_PER_USEC);SEC---纳秒
dispatch_after延时添加到队列
dispatch_time_t delayTime3 = dispatch_time(DISPATCH_TIME_NOW, 3*NSEC_PER_SEC);
dispatch_time_t delayTime2 = dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
NSLog(@"current task");
dispatch_after(delayTime3, mainQueue, ^{
NSLog(@"3秒之后添加到队列");
});
dispatch_after(delayTime2, mainQueue, ^{
NSLog(@"2秒之后添加到队列");
});
NSLog(@"next task");
2015-11-19 15:50:19.369 Whisper[2725:172593] current task
2015-11-19 15:50:19.370 Whisper[2725:172593] next task
2015-11-19 15:50:21.369 Whisper[2725:172593] 2秒之后添加到队列
2015-11-19 15:50:22.654 Whisper[2725:172593] 3秒之后添加到队列
dispatch_after只是延时提交block,并不是延时后立即执行,并不能做到精确控制,需要精确控制的朋友慎用哦
dispatch_apply在给定的队列上多次执行某一任务,在主线程直接调用会阻塞主线程去执行block中的任务。
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
NSLog(@"current task");
dispatch_async(globalQueue, ^{
dispatch_queue_t applyQueue = dispatch_get_global_queue(0, 0);
//第一个参数,3--block执行的次数
//第二个参数,applyQueue--block任务提交到的队列
//第三个参数,block--需要重复执行的任务
dispatch_apply(3, applyQueue, ^(size_t index) {
NSLog(@"current index %@",@(index));
sleep(1);
});
NSLog(@"dispatch_apply 执行完成");
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSLog(@"回到主线程更新UI");
});
});
NSLog(@"next task");
2015-11-19 16:24:45.015 Whisper[4034:202269] current task
2015-11-19 16:24:45.016 Whisper[4034:202269] next task
2015-11-19 16:24:45.016 Whisper[4034:202347] current index 0
2015-11-19 16:24:45.016 Whisper[4034:202344] current index 1
2015-11-19 16:24:45.016 Whisper[4034:202345] current index 2
2015-11-19 16:24:46.021 Whisper[4034:202347] dispatch_apply 执行完成
2015-11-19 16:24:46.021 Whisper[4034:202269] 回到主线程更新UI
dispatch_once保证在app运行期间,block中的代码只执行一次
ShareManager的.h文件
#import <Foundation/Foundation.h>
@interface ShareManager : NSObject
@property (nonatomic, copy) NSString *someProperty;
+ (ShareManager *)shareManager;
+ (ShareManager *)sharedManager;
@end
ShareManager的.m文件
#import "ShareManager.h"
@implementation ShareManager
static ShareManager *sharedManager = nil;
//GCD实现单例功能
+ (ShareManager *)shareManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
//在ARC下,非GCD,实现单例功能
+ (ShareManager *)sharedManager
{
@synchronized(self) {
if (!sharedManager) {
sharedManager = [[self alloc] init];
}
}
return sharedManager;
}
- (instancetype)init{
self = [super init];
if (self) {
_someProperty =@"Default Property Value";
}
return self;
}
@end
ShareManager的使用
#import "ShareManager.h"
在需要使用的函数中,直接调用下面的方法
ShareManager *share = [ShareManager sharedManager];
NSLog(@"share is %@",share.someProperty);
dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(conCurrentQueue, ^{
NSLog(@"dispatch 1");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"dispatch 2");
});
dispatch_barrier_async(conCurrentQueue, ^{
NSLog(@"dispatch barrier");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"dispatch 3");
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"dispatch 4");
});
2015-11-19 18:12:34.125 Whisper[22633:297257] dispatch 1
2015-11-19 18:12:34.125 Whisper[22633:297258] dispatch 2
2015-11-19 18:12:34.126 Whisper[22633:297258] dispatch barrier
2015-11-19 18:12:34.127 Whisper[22633:297258] dispatch 3
2015-11-19 18:12:34.127 Whisper[22633:297257] dispatch 4
标签:with 场景 notify 个人博客 char ash 函数 atom arc
原文地址:http://www.cnblogs.com/zxykit/p/6164063.html