GCD的简单使用,开辟一条新的线程,让上面的任务串行执行
2016-07-12 14:55
567 查看
1. 异步线程+串行队列 新开辟一条线程。串行执行
dispatch_queue_t chuanQue=dispatch_queue_create("chuan", DISPATCH_QUEUE_SERIAL);
dispatch_async(chuanQue, ^{
for (int i=0; i<50; i++) {
NSLog(@"%@A线程的%d",[NSThread currentThread],i);
}
});
dispatch_async(chuanQue, ^{
for (int i=0; i<50; i++) {
NSLog(@"%@B线程的%d",[NSThread currentThread],i);
}
});
2. 开辟一条异步线程,让他们并发执行
dispatch_async与并发队列配合
3. 下次并发队列我们不需要手动创建,直接使用系统自带的全局并发队列就可以了e
// 1.获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 2.将任务加入队列
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"1-----%@", [NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"2-----%@", [NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"3-----%@", [NSThread currentThread]);
}
});
dispatch_queue_t chuanQue=dispatch_queue_create("chuan", DISPATCH_QUEUE_SERIAL);
dispatch_async(chuanQue, ^{
for (int i=0; i<50; i++) {
NSLog(@"%@A线程的%d",[NSThread currentThread],i);
}
});
dispatch_async(chuanQue, ^{
for (int i=0; i<50; i++) {
NSLog(@"%@B线程的%d",[NSThread currentThread],i);
}
});
2. 开辟一条异步线程,让他们并发执行
dispatch_async与并发队列配合
3. 下次并发队列我们不需要手动创建,直接使用系统自带的全局并发队列就可以了e
// 1.获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 2.将任务加入队列
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"1-----%@", [NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"2-----%@", [NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"3-----%@", [NSThread currentThread]);
}
});
相关文章推荐
- JAVA8之初见
- Java JList用法二
- 华为HCIE理论与实操笔记【4-ssh管理】
- 详解zabbix安装部署(Server端篇)
- git 学习总结
- [EIDE助手插件]启动密码
- CXF对Interceptor拦截器的支持
- Tkinter教程之Grid篇
- HDU 1052 Tian Ji -- The Horse Racing
- Redis & Redis 桌面管理工具Redis Desktop Manager
- 《JAVA中的二进制》
- 常见编译错误
- Oracle 性能优化之内核的shmall 和shmmax 参数
- 文字上下轮播
- 房间安排
- HDU 1512 Monkey King 左偏树 + 并查集
- SpringMVC 返回String中文乱码
- Android异步消息处理机制详解及源码分析 Handler
- 【分享】WeX5的正确打开方式(7)——数据组件详解
- java.lang.String cannot be cast to [Ljava.lang.Object