dispatch_set_target_queue测试

 

dispatch_set_target_queue会将其他queue转为和targetqueue一样的优先级和执行时序,看下面的例子

即使queue1,queue2,queue3,都是并行的queue,但是输出结果仍然是串行的结果;

- (void)testTargetQueue
{
    dispatch_queue_t targetQueue = dispatch_queue_create("test.target.queue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue1 = dispatch_queue_create("test.1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue2 = dispatch_queue_create("test.2", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue3 = dispatch_queue_create("test.3", DISPATCH_QUEUE_CONCURRENT);
    dispatch_set_target_queue(queue1, targetQueue);
    dispatch_set_target_queue(queue2, targetQueue);
    dispatch_set_target_queue(queue3, targetQueue);
    
    dispatch_async(queue1, ^{
        NSLog(@"1 in");
        [NSThread sleepForTimeInterval:3.f];
        NSLog(@"1 out");
    });
    
    dispatch_async(queue2, ^{
        NSLog(@"2 in");
        [NSThread sleepForTimeInterval:2.f];
        NSLog(@"2 out");
    });
    dispatch_async(queue3, ^{
        NSLog(@"3 in");
        [NSThread sleepForTimeInterval:1.f];
        NSLog(@"3 out");
    });
}

  

2017-03-07 20:31:11.636 DispatchTargetQueueTest[42985:377250] 1 in

2017-03-07 20:31:14.711 DispatchTargetQueueTest[42985:377250] 1 out

2017-03-07 20:31:14.711 DispatchTargetQueueTest[42985:377250] 2 in

2017-03-07 20:31:16.784 DispatchTargetQueueTest[42985:377250] 2 out

2017-03-07 20:31:16.784 DispatchTargetQueueTest[42985:377250] 3 in

2017-03-07 20:31:17.859 DispatchTargetQueueTest[42985:377250] 3 out

posted on 2017-03-07 20:32  wxm5558  阅读(138)  评论(0编辑  收藏  举报