一、定义
/**
* 参数1: object 要变更的队列,不能是主队列和全局队列。
* 参数2: queue 目标队列
*/
void
dispatch_set_target_queue(dispatch_object_t object,
dispatch_queue_t _Nullable queue);
dispatch_set_target_queue函数有两个作用:
- 变更队列的优先级。
- 更改队列的目标队列。
二、变更队列的优先级
示例代码:
//创建一个默认串行队列,要变更优先级
dispatch_queue_t serviceQueue = dispatch_queue_create("com.mcq.change.priority", NULL);
//参照
dispatch_queue_t noServiceQueue = dispatch_queue_create("com.mcq.default", NULL);
//全局队列,DISPATCH_QUEUE_PRIORITY_BACKGROUND优先级
dispatch_queue_t globleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
//变更serviceQueue的优先级
dispatch_set_target_queue(serviceQueue, globleQueue);
//两个队列中,异步输出。
dispatch_async(serviceQueue, ^{
NSLog(@"after - 1");
});
dispatch_async(noServiceQueue, ^{
NSLog(@"after - 2");
});
//输出结果
after - 2
after - 1
由于noServiceQueue队列的优先级大于serviceQueue队列的优先级,所以先输出after - 2
,在输出after - 1。
三、变更队列的目标队列
示例代码:
//创建5个串行队列
dispatch_queue_t serialQueue1 = dispatch_queue_create("com.gcd.setTargetQueue.serial1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t serialQueue2 = dispatch_queue_create("com.gcd.setTargetQueue.serial2", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t serialQueue3 = dispatch_queue_create("com.gcd.setTargetQueue.serial3", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t serialQueue4 = dispatch_queue_create("com.gcd.setTargetQueue.serial4", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t serialQueue5 = dispatch_queue_create("com.gcd.setTargetQueue.serial5", DISPATCH_QUEUE_SERIAL);
//分别在串行队列中异步执行
dispatch_async(serialQueue1, ^{
NSLog(@"before - 1 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue2, ^{
NSLog(@"before - 2 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue3, ^{
NSLog(@"before - 3 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue4, ^{
NSLog(@"before - 4 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue5, ^{
NSLog(@"before - 5 - %@", [NSThread currentThread]);
});
//注意结果,分别在不同的线程中。
2018-06-11 11:20:47.253368+0800 GCD-dispatch_set_target_queue[45226:1685324] before - 1 - <NSThread: 0x604000462d00>{number = 4, name = (null)}
2018-06-11 11:20:47.253368+0800 GCD-dispatch_set_target_queue[45226:1685321] before - 2 - <NSThread: 0x604000464040>{number = 5, name = (null)}
2018-06-11 11:20:47.253396+0800 GCD-dispatch_set_target_queue[45226:1685323] before - 3 - <NSThread: 0x604000267700>{number = 3, name = (null)}
2018-06-11 11:20:47.253396+0800 GCD-dispatch_set_target_queue[45226:1685322] before - 4 - <NSThread: 0x60000067bec0>{number = 6, name = (null)}
2018-06-11 11:20:47.253411+0800 GCD-dispatch_set_target_queue[45226:1685362] before - 5 - <NSThread: 0x604000464d80>{number = 7, name = (null)}
//指定一个目标串行队列
dispatch_queue_t targetQueue = serialQueue1;
//设置目标串行队列
dispatch_set_target_queue(serialQueue2, targetQueue);
dispatch_set_target_queue(serialQueue3, targetQueue);
dispatch_set_target_queue(serialQueue4, targetQueue);
dispatch_set_target_queue(serialQueue5, targetQueue);
//分别在队列中异步执行任务。
dispatch_async(serialQueue1, ^{
NSLog(@"after - 1 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue2, ^{
NSLog(@"after - 2 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue3, ^{
NSLog(@"after - 3 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue4, ^{
NSLog(@"after - 4 - %@", [NSThread currentThread]);
});
dispatch_async(serialQueue5, ^{
NSLog(@"after - 5 - %@", [NSThread currentThread]);
});
//注意结果,打印的线程为同一个线程地址。
2018-06-11 11:25:28.762434+0800 GCD-dispatch_set_target_queue[45275:1689079] after - 1 - <NSThread: 0x60000046f540>{number = 4, name = (null)}
2018-06-11 11:25:28.763332+0800 GCD-dispatch_set_target_queue[45275:1689079] after - 4 - <NSThread: 0x60000046f540>{number = 4, name = (null)}
2018-06-11 11:25:28.764214+0800 GCD-dispatch_set_target_queue[45275:1689079] after - 2 - <NSThread: 0x60000046f540>{number = 4, name = (null)}
2018-06-11 11:25:28.764591+0800 GCD-dispatch_set_target_queue[45275:1689079] after - 3 - <NSThread: 0x60000046f540>{number = 4, name = (null)}
2018-06-11 11:25:28.765618+0800 GCD-dispatch_set_target_queue[45275:1689079] after - 5 - <NSThread: 0x60000046f540>{number = 4, name = (null)}
最后附上Demo地址: https://pan.baidu.com/s/1ih9NR4D99DvYJnkQIweiFw
密码: 5hrx
网友评论