iOS开发中的并发、串行队列,同步、异步任务
来源:互联网 发布:numpy攻略 源码 编辑:程序博客网 时间:2024/04/30 03:37
在多线程开发中我们经常会遇到这些概念:并发队列、串行队列、同步任务、异步任务。我们将这四个概念进行组合会有四种结果:串行队列+同步任务、串行队列+异步任务、并发队列+同步任务、并发队列+异步任务。我们对这四种结果进行解释:
1.串行队列+同步任务:不会开启新的线程,任务逐步完成。
2.串行队列+异步任务:开启新的线程,任务逐步完成。
3.并发队列+同步任务:不会开启新的线程,任务逐步完成。
4.并发队列+异步任务:开启新的线程,任务同步完成。
我们如果要让任务在新的线程中完成,应该使用异步线程。为了提高效率,我们还应该将任务放在并发队列中。因此在开发中使用最多的是并发队列+异步任务。看代码:
// 串行队列+同步任务
- (void)serialSyn{
dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"1---%@", [NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"2---%@", [NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"3---%@", [NSThreadcurrentThread]);
}
});
}
// 串行队列+异步任务
- (void)serialAsyn{
dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"1---%@", [NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"2---%@", [NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"3---%@", [NSThreadcurrentThread]);
}
});
}
// 并发队列+同步任务
- (void)concurrenSyn{
dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"1---%@", [NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"2---%@", [NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"3---%@", [NSThreadcurrentThread]);
}
});
}
// 并发队列+异步任务
- (void)concurrentAsyn{
dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"1---%@", [NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"2---%@", [NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i <3; i ++) {
NSLog(@"3---%@", [NSThreadcurrentThread]);
}
});
}
看表格:
- iOS开发中的并发、串行队列,同步、异步任务
- 串行队列/并发队列&异步任务/同步任务代码演练
- iOS -- 进程/多线程/同步任务/异步任务/串行队列/并行队列(对比分析)
- 进程/多线程/同步任务/异步任务/串行队列/并行队列
- iOS多线程同步异步、串行并发浅析
- iOS多线程并发,串行,同步,异步
- IOS串行队列、并行队列进行同步或者异步任务解析
- ios-day19-02(GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务)
- iOS GCD/主队列/并行队列/全局队列/串行队列/同步任务/异步任务区别 含代码
- 勘错-串行、并发队列与同步异步执行
- iOS学习笔记74-IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别附代码
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等(有示例代码)
- GCD 主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务
- ios菜鸟关于同步、异步 并发和串行的理解
- IOS 异步同步执行和并行串行队列的不解之缘
- android studio生成签名导打包的方法
- Zurmo(十一)Relation之static::OWNED和NOOWNED
- struts2 简介及使用步骤
- iOS-OC-基本控件之UIPageControl
- 从源码解析LinkedList集合
- iOS开发中的并发、串行队列,同步、异步任务
- 三次样条差值
- linked-list-cycle-ii (链表判环 并返回交点)
- jquery append()方法与html()方法用法区别
- Java DB loadBalance 设计
- 生产者消费者模式 详解
- 这是二叉搜索树吗?
- 单例模式
- 文章标题