各种方法
- 获取主线程队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
- 获取global子线程队列
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
关于子线程队列priority,执行完High,再执行Default,然后是Low,最后是Backgound。
- 创建自定义队列
dispatch_queue_t customQueue = dispatch_queue_create("com.uzai.testQueue", DISPATCH_QUEUE_CONCURRENT);
创建队列,serial串行执行,concurrent并行执行
- 异步执行
dispatch_async(customQueue, ^{
NSLog(@"custom serial queue %@",[NSThread currentThread]);
});
- 同步执行
dispatch_sync(customQueue, ^{
NSLog(@"custom serial queue %@",[NSThread currentThread]);
});
异步执行串行队列,同一子线程串行执行
异步执行并行队列,多个子线程并行执行
同步执行串行队列,主线程串行执行
同步执行并行队列,主线程串行执行
- 按次数执行block
按照指定次数将 block 添加到指定的队列当中,dispatch_apply函数会等待全部处理结束。
dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSArray *wordsArray = @[@"GCD",@"NSOperations",@"NSOperationQueues",@"algorithms"];
dispatch_apply(wordsArray.count, asyncQueue, ^(size_t currentIndex) {
NSLog(@"current size -> %@",wordsArray[currentIndex]);
});
NSLog(@"all applies are done");
- 创建单例
+ (instancetype)shareInstance {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- dispatch_group
- 使用dispatch_group_async在多个异步任务完成后,再执行dispatch_group_notify中的任务。
dispatch_queue_t current1 = dispatch_queue_create("current1", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, current1, ^{
sleep(3);
NSLog(@"current 1 finish %@",[NSThread currentThread]);
});
dispatch_group_async(group, current1, ^{
sleep(3);
NSLog(@"current 2 finish %@",[NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all finish");
});
//output
//2017-12-07 15:57:59.207 iOSInterviewProblems[64337:3876851] current 1 finish <NSThread: 0x60800026f6c0>{number = 9, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3945895] current 2 finish <NSThread: 0x600000270800>{number = 12, name = (null)}
//2017-12-07 15:58:02.282 iOSInterviewProblems[64337:3875012] all finish
- 使用 dispatch_group_enter,dispatch_group_leave 获取多个异步线程执行完成时候。
- dispatch_group_enter 在任务开始执行时调用
- dispatch_group_leave 在任务完成时调用
dispatch_group_t downloadGroup = dispatch_group_create();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.uzai.download", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 6; i++) {
dispatch_group_enter(downloadGroup);
dispatch_async(downloadQueue, ^{
NSLog(@"now downloading");
sleep(i);
dispatch_group_leave(downloadGroup);
NSLog(@"downloaded");
});
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
NSLog(@"all download done");
});
//output
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995991] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3995994] now downloading
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996044] downloaded
// 2017-12-07 17:06:24.765 iOSInterviewProblems[65166:3996047] now downloading
// 2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996044] now downloading
// 2017-12-07 17:06:24.766 iOSInterviewProblems[65166:3996048] now downloading
// 2017-12-07 17:06:25.840 iOSInterviewProblems[65166:3995991] downloaded
// 2017-12-07 17:06:26.766 iOSInterviewProblems[65166:3995994] downloaded
// 2017-12-07 17:06:27.840 iOSInterviewProblems[65166:3996047] downloaded
// 2017-12-07 17:06:28.766 iOSInterviewProblems[65166:3996044] downloaded
// 2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3996048] downloaded
// 2017-12-07 17:06:29.767 iOSInterviewProblems[65166:3995950] all download done
- dispatch_sources
GCD提供一系列的dispatch sources,用来监控活动的状况(底层系统活动,例如Unix 信号,VFS 节点 等等)。并且在上述活动发生的时候,执行提交到队列的事务。
-
创建GCDTimer
- gcd timer带有两个参数,interval代表的是触发时机,leeway代表的是允许的误差。例如设置interval = 1,leeway = 1。代表着1秒触发,可以有1秒的偏差。
- timer无法自动释放,需要通过dispatch_source_cancel进行释放。
//创建并启动timer
if (self.timerSource == nil) {
self.timerCountDown = 0;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timerSource, dispatch_walltime(NULL, 0), 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timerSource, ^{
self.timerCountDown++;
NSLog(@"printting now -> %d",self.timerCountDown);
});
dispatch_resume(self.timerSource);
}
//暂停timer
if (self.timerSource) {
dispatch_suspend(self.timerSource);
}
//取消掉timer
if (self.timerSource) {
dispatch_source_cancel(self.timerSource);
}
注意事项
- dispatch_sync 使用
该方法直到block执行完毕才会返回执行下一个任务。在当前队列调用会造成死锁。