//创建串行队列 DISPATCH_QUEUE_SERIAL
// 并行队列 DISPATCH_QUEUE_CONCURRENT
//0.
dispatch_queue_t serialDispatchQueue = dispatch_queue_create("blog.csdn.net/baitxaps", NULL);
//1.
dispatch_queue_t concurrentDispatchQueue = dispatch_queue_create("blog.csdn.com/baitxaps", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentDispatchQueue,^{
NSLog(@"\n~~~Block on concurrentDispatchQueue");
});
dispatch_block_t kk = ^{
DLog(@"\n~~~test");
};
dispatch_async(concurrentDispatchQueue, kk);
if !OS_OBJECT_USE_OBJC
//dispatch_release(serialDispatchQueue);
dispatch_release(concurrentDispatchQueue);
endif
//2.各种Dispatch Queue获取方法
/*
对于 Main Dispatch Queue和Global Dispatch Queue执行dispatch_retain、dispatch_release函数不会引用作何变化,
也不会有任何问题
*/
dispatch_queue_t mainDispatchQueue = dispatch_get_main_queue();//main Dispatch 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);//后台优先级
//3.默认优先级的Global Dispatch Queue中执行Block
dispatch_async(globalDispatchQueueDefault, ^{
// 并行执行的处理
//...
dispatch_async(mainDispatchQueue, ^{
//只能在主线程中执行的处理
//...
});
});
//4.指定变更执行优先级(在后台执行动作处理的Serial Dispatch Queue的生成方法)
dispatch_set_target_queue(serialDispatchQueue, globalDispatchQueueBackground);
//5.在指定时间后执行处理的情况,用dispatch_after
dispatch_time_t time =dispatch_time(DISPATCH_TIME_NOW, 3ull *NSEC_PER_SEC);
dispatch_after(time, mainDispatchQueue, ^{
NSLog(@"waited at least 3 seconds.");
});
//6.dispatch_walltime()
dispatch_time_t seconds = getDispatchTimeByDate([NSDate dateWithTimeIntervalSinceNow:4]);
NSLog(@"second = %llu",seconds);
dispatch_after(seconds,mainDispatchQueue,^{
NSLog(@"waited at least 4 seconds.");
});
//7.Dispatch Group
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, globalDispatchQueueDefault, ^{NSLog(@"blk0");});
dispatch_group_async(group, globalDispatchQueueDefault, ^{NSLog(@"blk1");});
dispatch_group_async(group, globalDispatchQueueDefault, ^{NSLog(@"blk2");});
//异步等待组里任务全部完成再执行
dispatch_group_notify(group, mainDispatchQueue, ^{NSLog(@"done");});
if !OS_OBJECT_USE_OBJC
dispatch_release(group);
endif
//7.dispatch_group_wait()
dispatch_group_t group_wait = dispatch_group_create();
dispatch_group_async(group_wait, globalDispatchQueueDefault, ^{NSLog(@"blk0");});
dispatch_group_async(group_wait, globalDispatchQueueDefault, ^{NSLog(@"blk1");});
dispatch_group_async(group_wait, globalDispatchQueueDefault, ^{NSLog(@"blk2");});
/// 同步等待一定时间队列操作完成
dispatch_group_wait(group_wait, DISPATCH_TIME_FOREVER);
if !OS_OBJECT_USE_OBJC
dispatch_release(group_wait);
endif
//8.
time = dispatch_time(DISPATCH_TIME_NOW,1ull *NSEC_PER_SEC);
dispatch_group_t group_result = dispatch_group_create();
long result = dispatch_group_wait(group_result, time);
// long result = dispatch_group_wait(group_result, DISPATCH_TIME_NOW);
if (result == 0) {
//属于Dispatch Group的全部内容处理执行结束
}else{
//属于Dispatch Group的某一个处理还在执行中
}
//9.dispatch_barrier_async()
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk0 for reading");});
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk1 for reading");});
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk2 for reading");});
// 在并行队列中等待前面的任务执行完再执行
dispatch_barrier_async(concurrentDispatchQueue, ^{NSLog(@"blk3 for writing");});
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk4 for reading");});
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk5 for reading");});
dispatch_async(concurrentDispatchQueue, ^{NSLog(@"blk6 for reading");});
//10.dispatch_apply()
//0> 将指定的block globalDispatchQueueDefault 添加到dispatch_apply 执行10次
dispatch_apply(10, globalDispatchQueueDefault, ^(size_t index) {
NSLog(@"dispatch_apply = %zu",index);
});
NSLog(@"dispatch_apply() done.");
//1>
NSArray *array = self.datas;
dispatch_apply(self.datas.count, globalDispatchQueueDefault, ^(size_t index){
NSLog(@"index= %zu,element = %@",index,array[index]);
});
//3>在Global Dispatch Queue中非同步执行
dispatch_async(globalDispatchQueueDefault, ^{
//Global Dispatch Queue 等待dispatch_apply函数中全部处理执行结束
dispatch_apply(self.datas.count, globalDispatchQueueDefault, ^(size_t index){
NSLog(@"index= %zu,element = %@",index,array[index]);
});
//dispatch_apply()中处理全部执行结束
//在Main Dispatch Queue中执行处理用户界用更新等
dispatch_async(mainDispatchQueue, ^{NSLog(@"在Main Dispatch Queue中执行处理用户界用更新等...,Done");});
});
//11.dispatch_suspend()/dispatch_resume()
/*
这些函数对已经执行的处理没有影响,挂起后,追加到Dispatch Queue中但尚未执行的处理在些之扣停止执行,
而恢复则使用这些处理能够继续执行。
*/
dispatch_suspend(globalDispatchQueueDefault);
dispatch_resume(globalDispatchQueueDefault);
//12.dispatch Semaphore 相当于NSOperationQueue的锁
//Dispatch Semaphore的计数初始值设定为1,保证可访问的NSArray类的对象的线程同时只能有1个
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
//1>
{
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i< 100000; i++) {
dispatch_async(globalDispatchQueueDefault, ^{
//等待Dispatch Semaphore,一直等待,直到Dispatch Semaphore的计数值达到大于等于1。
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//执行完0其他线程进不来
/*
Dispatch Semaphore的计数值达到大于等于1,所以将Dispatch Semaphore的计数值减去1
dispatch_semaphore_wait()执行返回。即执行到此进的Dispatch Semaphore的计数值恒
为0,由于可访问NSArray类对象的线程只有1个,因此可安全地进行更新。
*/
[array addObject:@(i)];
/*
排他控制处理结束,所以通过dispatch_semaphore_signal()将Dispatch Semaphore的计
数值加1。如果有通过dispatch_semaphore_wait通过()等待Dispatch Semaphore和计数值
增加的线程,就由最先等待的线程执行。
*/
dispatch_semaphore_signal(semaphore);//你操作完,+1让别人好进来啊
});
}
}
if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
endif
//13.dispatch_once()
//更新标志变量
static int initialized = NO;
if(initialized == NO){
initialized = YES;
}
//可通过dispatch_once()简化
static dispatch_once_t pred;
dispatch_once(&pred,^{
initialized = YES;
});
/*
通过dispatch_once(),该源代码即使在多线程环境下执行,可保证100%安全
之前的源代码在大多数情况下也是安全的,但在多核CPU中,在正在更新表示是否初始化的标志变量时读取,就有
可能多次执行初始化处理。而用dispatch_once()初始化就不必担心这样的问题。
这就是所说的单例模式,在生成单例对象时使用。
*/
//14.死锁下面几种情况:
/*
//1>
dispatch_sync(mainDispatchQueue, ^{NSLog(@"死锁1");});//同步下再掉主队列GG
//2>
dispatch_async(mainDispatchQueue, ^{
dispatch_sync(mainDispatchQueue, ^{NSLog(@"死锁2");});
});
//3>
dispatch_async(serialDispatchQueue, ^{
dispatch_sync(serialDispatchQueue, ^{NSLog(@"死锁3");});
});
*/
}