Dispatch Queue的种类
Serial Dispatch Queue 等待现在执行中处理结束 (按照追加的顺序, 先进先出执行).
创建Serial Dispatch Queue
dispatch_queue_t queue1 = dispatch_queue_create("queue1", NULL);
- 追加多个处理
dispatch_async(queue1, ^{
NSLog(@"1");
});
dispatch_async(queue1, ^{
NSLog(@"2");
});
dispatch_async(queue1, ^{
NSLog(@"3");
});
dispatch_async(queue1, ^{
NSLog(@"4");
});
dispatch_async(queue1, ^{
NSLog(@"5");
});
dispatch_async(queue1, ^{
NSLog(@"6");
});
- 执行后的结果
2016-07-30 21:08:18.689 GCD[4114:268510] 1
2016-07-30 21:08:18.689 GCD[4114:268510] 2
2016-07-30 21:08:18.689 GCD[4114:268510] 3
2016-07-30 21:08:18.690 GCD[4114:268510] 4
2016-07-30 21:08:18.690 GCD[4114:268510] 5
2016-07-30 21:08:18.690 GCD[4114:268510] 6
有打印的结果可知道, Serial Dispatch Queue每次只能执行一个任务.
- 当创建多个Serial Dispatch Queue的时候也可以达成类似Concurrent Dispatch Queue的效果, 但是创建过多你线程的话会消耗大量的内存呢和CPU资源. 下列代码:
dispatch_queue_t queue1 = dispatch_queue_create("queue1", NULL); dispatch_queue_t queue2 = dispatch_queue_create("queue2", NULL); dispatch_queue_t queue3 = dispatch_queue_create("queue3", NULL); dispatch_queue_t queue4 = dispatch_queue_create("queue4", NULL);
- 追加任务
dispatch_async(queue1, ^{ NSLog(@"1"); }); dispatch_async(queue2, ^{ NSLog(@"2"); }); dispatch_async(queue3, ^{ NSLog(@"3"); }); dispatch_async(queue4, ^{ NSLog(@"4"); });
- 打印出来的结果应该是无序的
2016-07-30 21:16:22.883 GCD[4165:273436] 2
2016-07-30 21:16:22.883 GCD[4165:273478] 4
2016-07-30 21:16:22.883 GCD[4165:273443] 1
2016-07-30 21:16:22.883 GCD[4165:273469] 3
- 一般的在更新相同数据资源导致数据竞争的时候会用到Serial Dispatch Queue
- Concurrent DisPatch Queue 不等待现在执行中结束 (并行执行).
- 创建Concurrent Dispatch Queue
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(concurrentQueue, ^{NSLog(@"1");}); dispatch_async(concurrentQueue, ^{NSLog(@"2");}); dispatch_async(concurrentQueue, ^{NSLog(@"3");}); dispatch_async(concurrentQueue, ^{NSLog(@"4");}); dispatch_async(concurrentQueue, ^{NSLog(@"5");});
- 执行结果
2016-07-30 21:25:19.923 GCD[4221:278303] 3 2016-07-30 21:25:19.923 GCD[4221:278155] 2 2016-07-30 21:25:19.923 GCD[4221:278162] 1 2016-07-30 21:25:19.923 GCD[4221:278311] 4 2016-07-30 21:25:19.923 GCD[4221:278312] 5
通过执行的结果可看出Concurrent Dispatch Queue的执行方式
#### Main Dispatch Queue / Global Dispatch Queue
- s
```
dispatch_queue_t highGlobalQueu = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(highGlobalQueue, ^{NSLog(@"1");});
dispatch_async(highGlobalQueue, ^{NSLog(@"2");});
dispatch_async(highGlobalQueue, ^{NSLog(@"3");});
dispatch_async(highGlobalQueue, ^{NSLog(@"4");});
dispatch_async(highGlobalQueue, ^{NSLog(@"5");});
```
- 四个优先级
define DISPATCH_QUEUE_PRIORITY_HIGH 2
define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
define DISPATCH_QUEUE_PRIORITY_LOW (-2)
define DISPATCH_QUEUE_PRIORITY_BACKGROUND
####dispatch_set_target_queue
- 优先级
// 第一个参数是要变更优先级的Queue, 第二个是被指定用到其优先级的参数
dispatch_queue_t highGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t mySerialQueue = dispatch_queue_create("mySerialQueue", NULL);
dispatch_set_target_queue(mySerialQueue, highGlobalQueue);
这个方法可以变更Queue的优先级, 还可以作为Dispatch Queue的执行阶层. Main Dispatch Queue 和 Global Dispatch Queue不能作为第一个参数. 如果在多个Serial Dispatch Queue中指定一个目标, 那么原先在执行的多个Serial Dispatch Queue, 在目标Serial Dispatch Queue上只能执行一个处理. 下面用代码实现一下:
dispatch_queue_t queue1 = dispatch_queue_create("queue1", NULL);
dispatch_queue_t queue2 = dispatch_queue_create("queue2", NULL);
dispatch_set_target_queue(queue1, queue2);
// 打印一下
dispatch_async(queue1, ^{
NSLog(@"1");
});
dispatch_async(queue2, ^{
NSLog(@"2");
});
打印结果
2016-07-30 22:32:26.134 GCD[4621:305219] 1
2016-07-30 22:32:26.135 GCD[4621:305219] 2
我们会发现结果是顺序排下来的, 这就说明在运行的过程中, Serial Dispatch Queue 只同时执行了一个处理