-
dispatch_set_target_queue
- 可设置队列的优先级
- 可设置队列的执行阶层
dispatch_set_target_queue(myQueue, targetQueue); 设置优先级 // 目标队列可以是 1. dispatch_get_mian_queue 2. dispatch_get_global_queue // 当设置为`dispatch_get_global_queue`, 则执行阶层还是原来自身的, 优先级与目标队列一致 // 当设置为`dispatch_get_mian_queue`, 执行阶层, 优先级都与目标队列一致 设置执行阶层 1. dispatch_get_mian_queue 主队列 2. dispatch_get_global_queue 全局并发队列 3. dispatch_queue_create("targetQueue", DISPATCH_QUEUE_CONCURRENT) 并发队列 4. dispatch_queue_create("targetQueue", DISPATCH_QUEUE_SERIAL) 串行队列
-
优先级由
dispatch_get_mian_queue
,dispatch_get_global_queue
设定 -
执行阶层, 由自身与目标队列共同决定
串行队列完全碾压并发队列
- test code
// 创建的队列是串行队列
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
// 目标队列是全局并发队列
dispatch_queue_t targetQueue = dispatch_get_global_queue(0, 0);
dispatch_set_target_queue(queue, targetQueue);
for (NSInteger i = 0; i < 100; i++) {
dispatch_async(queue, ^{
if (i % 2 == 0) {
[NSThread sleepForTimeInterval:3];
}
NSLog(@"%@ - %ld", [NSThread currentThread], i);
});
}
// 输出
2017-08-08 10:28:15.625 breadTrip[3710:419701] <NSThread: 0x608000269400>{number = 3, name = (null)} - 0
2017-08-08 10:28:15.625 breadTrip[3710:419701] <NSThread: 0x608000269400>{number = 3, name = (null)} - 1
2017-08-08 10:28:18.629 breadTrip[3710:419701] <NSThread: 0x608000269400>{number = 3, name = (null)} - 2
2017-08-08 10:28:18.629 breadTrip[3710:419701] <NSThread: 0x608000269400>{number = 3, name = (null)} - 3
- 结论:
- queue的优先级与targetQueue一致
- queue的执行阶层还是自身的串行队列
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t targetQueue = dispatch_get_global_queue(0, 0);
dispatch_set_target_queue(queue, targetQueue);
- 结论:
- queue的优先级与targetQueue一致
- queue的执行阶层与targetQueue一致是的并发队列
dispatch_queue_t queue1 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue2 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t targetQueue = dispatch_get_main_queue();
dispatch_set_target_queue(queue1, targetQueue);
dispatch_set_target_queue(queue2, targetQueue);
- 结论:
- queue1, queue2 的优先级与targetQueue一致
- queue1, queue2 的执行阶层与targetQueue一致是的串行队列
- queue1, queue2追加任务都会在主线程执行
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t targetQueue = dispatch_queue_create("targetQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(queue, targetQueue);
- 结论:
- queue的优先级与targetQueue一致
- queue的执行阶层不变还是
串行队列
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t targetQueue = dispatch_queue_create("targetQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(queue, targetQueue);
- 结论:
queue的优先级与targetQueue一致
queue的执行阶层与targetQueue一致, 并发队列
dispatch_queue_t queue1 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue2 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t targetQueue = dispatch_queue_create("targetQueue", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(queue, targetQueue);
- 结论:
queue1, queue2 的优先级与targetQueue一致
queue1, queue2 的执行阶层与targetQueue一致是的串行队列
dispatch_queue_t queue1 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t targetQueue = dispatch_queue_create("targetQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(queue, targetQueue);
- 结论:
queue1, queue2 的优先级与targetQueue一致
queue2 的执行阶层与targetQueue一致是的串行队列
queue1 的执行阶层不变还是串行队列
总结
- 优先级有目标队列一致
- 当自身为并发队列,则执行阶层与目标队列一致
- 当自身为串行队列,则执行阶层不变
- 目标队列为
主对列
时, 执行阶层与主队列一致
网友评论