GCD几种简单用法
2016-04-09 13:56
162 查看
//1.同步 顺序执行
- (void)test1
{
dispatch_async(mainQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_async(mainQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_async(mainQueue,
^{
[self doSomeThing:@"C"];
});
}
//2.异步同时进行
- (void)test2
{
dispatch_async(globalQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_async(globalQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_async(globalQueue,
^{
[self doSomeThing:@"C"];
});
}
//3.A B同时进行 最后执行C
- (void)test3
{
dispatch_group_t group
= dispatch_group_create();
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"C"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"D"];
});
dispatch_group_notify(group, globalQueue,
^{
[self doSomeThing:[NSString stringWithFormat: @"最后执行----%d",[NSThread isMainThread]]];
dispatch_async(mainQueue,
^{
[self doSomeThing:[NSString stringWithFormat: @"主线程刷新UI----%d",[NSThread isMainThread]]];
});
});
}
//4.自定义队列
自定义执行顺序
可串行
可并行
- (void)test4
{
dispatch_queue_t customQueue = dispatch_queue_create("anfu",
DISPATCH_QUEUE_CONCURRENT);
dispatch_async(customQueue, ^{
[self
doSomeThing:@"A"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"B"];
});
dispatch_barrier_async(customQueue, ^{
[self
doSomeThing:@"C"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"D"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"E"];
});
}
//5.延迟调用
- (void)test5
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(1 *NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
[self doSomeThing:@"过1秒后执行"];
});
}
//6.调用多次
- (void)test6
{
dispatch_apply(10, globalQueue, ^(size_t time)
{
NSLog(@"%zu",time);
});
}
//7.只调用一次,一般在单例中使用
- (void)test7
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,
^{
NSLog(@"只调用一次");
});
}
//公共调用方法
- (void)doSomeThing:(NSString *)obj
{
sleep(1);
NSLog(@"%@",obj);
}
- (void)test1
{
dispatch_async(mainQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_async(mainQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_async(mainQueue,
^{
[self doSomeThing:@"C"];
});
}
//2.异步同时进行
- (void)test2
{
dispatch_async(globalQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_async(globalQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_async(globalQueue,
^{
[self doSomeThing:@"C"];
});
}
//3.A B同时进行 最后执行C
- (void)test3
{
dispatch_group_t group
= dispatch_group_create();
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"A"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"B"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"C"];
});
dispatch_group_async(group, globalQueue,
^{
[self doSomeThing:@"D"];
});
dispatch_group_notify(group, globalQueue,
^{
[self doSomeThing:[NSString stringWithFormat: @"最后执行----%d",[NSThread isMainThread]]];
dispatch_async(mainQueue,
^{
[self doSomeThing:[NSString stringWithFormat: @"主线程刷新UI----%d",[NSThread isMainThread]]];
});
});
}
//4.自定义队列
自定义执行顺序
可串行
可并行
- (void)test4
{
dispatch_queue_t customQueue = dispatch_queue_create("anfu",
DISPATCH_QUEUE_CONCURRENT);
dispatch_async(customQueue, ^{
[self
doSomeThing:@"A"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"B"];
});
dispatch_barrier_async(customQueue, ^{
[self
doSomeThing:@"C"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"D"];
});
dispatch_async(customQueue, ^{
[self
doSomeThing:@"E"];
});
}
//5.延迟调用
- (void)test5
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(1 *NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
[self doSomeThing:@"过1秒后执行"];
});
}
//6.调用多次
- (void)test6
{
dispatch_apply(10, globalQueue, ^(size_t time)
{
NSLog(@"%zu",time);
});
}
//7.只调用一次,一般在单例中使用
- (void)test7
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,
^{
NSLog(@"只调用一次");
});
}
//公共调用方法
- (void)doSomeThing:(NSString *)obj
{
sleep(1);
NSLog(@"%@",obj);
}
相关文章推荐
- 谷歌新书:SRE
- [IO]——打印流
- 求两个整数之间的汉明距离---Salem
- 【UFLDL-exercise3&4-PCA and Whitening】
- 第七周上机实践项目-项目一-线段类-成员函数
- Unity组件的基类单例模式
- MongoDB shell脚本 自动添加权限与账号密码
- “Enterprise Architect”和数据库的不解之缘
- 以对象的方式来访问xml数据表(三)
- 自定义cell中的按钮点击事件
- Sublime Text 3实用快捷键大全
- 【算法】普通方法和筛选法求素数
- mysql 数据类型选择
- Android 的SQLite
- 【翻译】Emmet (ZenCoding) 缩写语法
- 用jQuery前,先看看jQuery库文件件导入没!
- 文件的读写与访问
- C#判断网络IP是否通畅
- Java中实现链式操作的简单例子
- hdu 1874 畅通工程续