一、Dispatch Queue
// 1.dispatch_queue_create:生成的dispatch_queue_t
dispatch_queue_t mySerialiDispatchQueue = dispatch_queue_create("com.example.gcd.MySerialiDispatchQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t myConcurrentDispatchQueue = dispatch_queue_create("com.example.gcd.MyConcurrentDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
// 2.dispatch_get_main_queue \ dispatch_get_global_queue
dispatch_queue_t mainDispatchQueue = dispatch_get_main_queue();
dispatch_queue_t globalDispatchQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t globalDispatchQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t globalDispatchQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_queue_t globalDispatchQueueBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
二、基本概念:异步 同步 串行队列 并发队列
dispatch_async: 异步,队列中的任务无需执行完毕,就可执行后续代码
dispatch_sync: 同步,队列中的任务执行完毕之后才可以执行后续代码
mySerialiDispatchQueue: 串行队列,队列中的是有顺序的执行完毕
myConcurrentDispatchQueue: 并发队列,队列中的任务谁执行的快谁先完成
三、dispatch_set_target_queue:为dispatch_queue_create生成的队列设置优先级
dispatch_set_target_queue(mySerialiDispatchQueue, globalDispatchQueueHigh);
四、dispatch_after
// 不是在x秒后执行DISPATCH_AFTER_BLOCK,而是x秒后把DISPATCH_AFTER_BLOCK追加到mainQueue
#define JAX_dispatch_after(x, dispatch_after_block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((x) * NSEC_PER_SEC)), dispatch_get_main_queue(), dispatch_after_block);
JAX_dispatch_after(10.0, ^{
NSLog(@"123");
})
五、dispatch_group
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, globalDispatchQueueDefault, ^{
[NSThread sleepForTimeInterval:3.0];
NSLog(@"1");
});
dispatch_group_async(group, globalDispatchQueueDefault, ^{
[NSThread sleepForTimeInterval:5.0];
NSLog(@"2");
});
dispatch_group_async(group, globalDispatchQueueDefault, ^{
[NSThread sleepForTimeInterval:1.0];
NSLog(@"3");
});
// dispatch_group_notify: 对多个处理全部结束之后再执行结束处理
dispatch_group_notify(group, mainDispatchQueue, ^{
NSLog(@"main");
});
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3.5 * NSEC_PER_SEC);
long result = dispatch_group_wait(group, time);
if (result == 0) {
NSLog(@"等待时间里任务全部完成");
} else {
NSLog(@"等待时间里任有未完成的任务");
}
//3
//1
//等待时间里任有未完成的任务
//2
//main
六、dispatch_barrier_async: 队列中的其他任务执行完成才会执行该任务。该任务完成才会去执行后续追加的任务。
七、dispatch_apply: 按指定的次数将指定的block追加到的dispatchQueue中
NSArray *array = @[@100, @200, @300];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 把3个block追加到queue中,执行不分先后顺序
dispatch_apply(array.count, globalDispatchQueueHigh, ^(size_t index) {
NSLog(@"%@", array[index]);
});
// 此时执行所有block执行结束
// 返回主线程刷新UI
dispatch_async(mainDispatchQueue, ^{
NSLog(@"updateUI");
});
});
八、dispatch_suspend \ dispatch_resume 队列挂起、恢复
九、dispatch_once: 执行只需运行一次的线程安全代码,单例
+ (instancetype)sharedInstance {
static JAXClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
十、dispatch semaphore
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 计数设为1,保证可以访问arry的线程同时只能有一个
dispatch_semaphore_t sempahore = dispatch_semaphore_create(1);
NSMutableArray *array_semaphore = [NSMutableArray array];
for (int i = 0; i < 1000; ++i) {
dispatch_async(queue, ^{
// 等待sempahore、直到sempahore大于等于1, 一旦计数器大于或等于1,计数器-1
dispatch_semaphore_wait(sempahore, DISPATCH_TIME_FOREVER);
[array_semaphore addObject:@(i)];
// 计数器+1
dispatch_semaphore_signal(sempahore);
});
}