设置使用dispatch_queue_create创建的队列的优先级

时间:2015-11-17 07:37:04

标签: ios objective-c multithreading grand-central-dispatch dispatch-async

我正在尝试使用dispatch_queue_create创建并发/串行队列,并将其优先级设置为" HIGH "通过将高优先级队列设置为其目标队列。但是,控制台上的结果是意外的。

为什么会发生这种情况?在默认和低优先级队列任务之前,不应该执行我的自定义队列任务吗?

-(void)callWebServiceUsername:(NSString *)username Password:(NSString*)password CompletionHandler:(void (^)(NSDictionary* dic, NSError *err))compHandler
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - first");
    });

    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

    dispatch_async(q, ^{
        NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_LOW - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_BACKGROUND - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_LOW - second");
    });

    dispatch_queue_t queueConcurrent = dispatch_queue_create("MyCONCURRENTQueue", DISPATCH_QUEUE_CONCURRENT);
    __weak dispatch_queue_t weakqueueConcurrent = queueConcurrent;

    dispatch_async(weakqueueConcurrent, ^(void){
        const char *label = dispatch_queue_get_label(weakqueueConcurrent);
        NSString *str = [NSString stringWithUTF8String:label];
        NSLog(@"%@",str);
        dispatch_async(dispatch_get_main_queue(), ^{
            compHandler([[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:str] forKeys:[NSArray arrayWithObject:@"Label"]],NULL);
        });
    });

    dispatch_queue_t queueSerial = dispatch_queue_create("MySERIALQueue", DISPATCH_QUEUE_SERIAL);
    __weak dispatch_queue_t weakqueueSerial = queueSerial;

    dispatch_async(weakqueueSerial, ^(void){

        const char *label = dispatch_queue_get_label(weakqueueSerial);
        NSString *str = [NSString stringWithUTF8String:label];
        NSLog(@"%@",str);
        dispatch_async(dispatch_get_main_queue(), ^{
            compHandler([[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:str] forKeys:[NSArray arrayWithObject:@"Label"]],NULL);
        });
    });

    dispatch_set_target_queue(weakqueueSerial, q);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - third");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_BACKGROUND - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - fourth");
    });
}

Console上的输出是:

2015-11-17 12:48:51.954 WebServiceCall[3745:98566] DISPATCH_QUEUE_PRIORITY_HIGH - second
2015-11-17 12:48:51.954 WebServiceCall[3745:98514] DISPATCH_QUEUE_PRIORITY_HIGH - first
2015-11-17 12:48:51.954 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - first
2015-11-17 12:48:51.956 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - third
2015-11-17 12:48:51.956 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - fourth
2015-11-17 12:48:51.954 WebServiceCall[3745:98530] DISPATCH_QUEUE_PRIORITY_DEFAULT - second
2015-11-17 12:48:51.955 WebServiceCall[3745:98567] MyCONCURRENTQueue
2015-11-17 12:48:51.954 WebServiceCall[3745:98565] MySERIALQueue
2015-11-17 12:48:51.955 WebServiceCall[3745:98568] DISPATCH_QUEUE_PRIORITY_LOW - second
2015-11-17 12:48:51.954 WebServiceCall[3745:98532] DISPATCH_QUEUE_PRIORITY_LOW - first
2015-11-17 12:48:51.957 WebServiceCall[3745:98568] DISPATCH_QUEUE_PRIORITY_BACKGROUND - second
2015-11-17 12:48:51.957 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_BACKGROUND - first

结果不应该是这样的:

2015-11-17 12:48:51.954 WebServiceCall[3745:98566] DISPATCH_QUEUE_PRIORITY_HIGH - second
    2015-11-17 12:48:51.954 WebServiceCall[3745:98514] DISPATCH_QUEUE_PRIORITY_HIGH - first
2015-11-17 12:48:51.954 WebServiceCall[3745:98565] MySERIALQueue

0 个答案:

没有答案