整理GCD用法
1.// 后台执行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
2.// 主线程执行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
});
3.// 一次性执行:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// code to be executed once
});
4.// 延迟2秒执行:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on the main queue after delay
});
5.// 自定义dispatch_queue_t
dispatch_queue_t urls_queue = dispatch_queue_create(“blog.devtang.com”, NULL);
dispatch_async(urls_queue, ^{
// your code
});
dispatch_release(urls_queue);
6.// 合并汇总结果
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行执行的线程一
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行执行的线程二
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// 汇总结果
});
===============
7.dispatch_barrier_async//已经开始执行的block将会继续执行,当dispatch_barrier_async开始执行的时候其他的block处于等待状态,dispatch_barrier_async的任务执行完后,其后的block才会执行
dispatch_apply//将一个指定的block执行指定的次数
8.//暂停
dispatch_suspend(globalQueue)
9.//恢复
dispatch_resume(globalQueue)
Dispatch Semaphore