前提:dispatch_queue_create创建的Dispatch Queue都是使用默认优先级别的线程。
问题一、dispatch_set_target_queue的作用?
1、变更dispatch_queue_create函数生成的Dispatch Queue的执行优先级
2、作为Dispatch Queue的执行阶层。
dispatch_set_target_queue(dispatch_object_t object, dispatch_queue_t queue);
第一个参数:
要变更执行优先级的Dispatch Queue,不能为Main Dispatch Queue和Global Dispatch Queue。
第二个参数:
要使用的执行优先级相同优先级的Global Dispatch Queue。
变更优先级
// 要变更优先级的串行队列,初始是默认优先级
dispatch_queue_t serialQueue = dispatch_queue_create("com.gcd.setTargetQueue.serialQueue", NULL);
// 获取优先级为后台优先级的全局队列
dispatch_queue_t globalDefaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
// 变更优先级
dispatch_set_target_queue(serialQueue, globalDefaultQueue);
作为执行阶层
多个Serial Dispatch Queue用dispatch_set_target_queue函数指定目标(第二个参数)为某一个Serial Dispatch Queue。那原先并行执行的多个Serial Dispatch Queue,在目标Serial Dispatch Queue上只能同时执行一个处理,这样可以防止处理并行执行。
设置执行阶层前:
dispatch_queue_t serialQueue1 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue1", NULL);
dispatch_queue_t serialQueue2 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue2", NULL);
dispatch_queue_t serialQueue3 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue3", NULL);
dispatch_queue_t serialQueue4 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue4", NULL);
dispatch_queue_t serialQueue5 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue5", NULL);
dispatch_async(serialQueue1, ^{
NSLog(@"1");
});
dispatch_async(serialQueue2, ^{
NSLog(@"2");
});
dispatch_async(serialQueue3, ^{
NSLog(@"3");
});
dispatch_async(serialQueue4, ^{
NSLog(@"4");
});
dispatch_async(serialQueue5, ^{
NSLog(@"5");
});
输出:
2018-07-22 18:37:51.908107+0800 Demo[1801:121342] 2
2018-07-22 18:37:51.908107+0800 Demo[1801:121343] 3
2018-07-22 18:37:51.908107+0800 Demo[1801:121341] 1
2018-07-22 18:37:51.908148+0800 Demo[1801:121344] 4
2018-07-22 18:37:51.908169+0800 Demo[1801:121352] 5
设置执行阶层后:
dispatch_queue_t serialQueue1 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue1", NULL);
dispatch_queue_t serialQueue2 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue2", NULL);
dispatch_queue_t serialQueue3 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue3", NULL);
dispatch_queue_t serialQueue4 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue4", NULL);
dispatch_queue_t serialQueue5 = dispatch_queue_create("com.gcd.setTargetQueue2.serialQueue5", NULL);
//创建目标串行队列
dispatch_queue_t targetSerialQueue = dispatch_queue_create("com.gcd.setTargetQueue2.targetSerialQueue", NULL);
//设置执行阶层
dispatch_set_target_queue(serialQueue1, targetSerialQueue);
dispatch_set_target_queue(serialQueue2, targetSerialQueue);
dispatch_set_target_queue(serialQueue3, targetSerialQueue);
dispatch_set_target_queue(serialQueue4, targetSerialQueue);
dispatch_set_target_queue(serialQueue5, targetSerialQueue);
dispatch_async(serialQueue1, ^{
NSLog(@"1");
});
dispatch_async(serialQueue2, ^{
NSLog(@"2");
});
dispatch_async(serialQueue3, ^{
NSLog(@"3");
});
dispatch_async(serialQueue4, ^{
NSLog(@"4");
});
dispatch_async(serialQueue5, ^{
NSLog(@"5");
});
输出:
2018-07-22 18:41:30.403676+0800 Demo[1843:123760] 1
2018-07-22 18:41:30.403893+0800 Demo[1843:123760] 2
2018-07-22 18:41:30.405137+0800 Demo[1843:123760] 3
2018-07-22 18:41:30.407517+0800 Demo[1843:123760] 4
2018-07-22 18:41:30.409133+0800 Demo[1843:123760] 5
总结:
变更优先级,dispatch_set_target_queue函数第二个参数是一个Global Dispatch Queue,第一个参数的Serial Dispatch Queue的优先级变更为Global Dispatch Queue的优先级。
作为执行阶层,多个Serial Dispatch Queue用dispatch_set_target_queue函数指定第二个参数为同一个Serial Dispatch Queue。