iOS 有四个任务ABCD,先执行AB,再执行CD

2018-11-27  本文已影响38人  封丑儿

一道经典的面试题:有四个任务ABCD,先执行AB,再执行CD

首先推荐两位大牛写的关于多线程的文章 很不错
传送门一:https://www.jianshu.com/p/4b1d77054b35
传送门二:http://www.cocoachina.com/ios/20170829/20404.html

我感觉有五种方法吧(抛砖引玉哈):
1.0 串行即可:(这个简单 不贴代码了)

2.0利用GCD中的栅栏函数,代码如下:

-(void)doBarrier{
    dispatch_queue_t queue = dispatch_queue_create("com.test.sx", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"AAAAAA");
    });
    dispatch_async(queue, ^{
        NSLog(@"BBBBBB");
    });
    
    dispatch_barrier_sync(queue, ^{
        NSLog(@"");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"CCCCCC");
    });
    dispatch_async(queue, ^{
        NSLog(@"DDDDDD");
    });
}

3.0 利用GCD中的 队列组和notify

-(void)doGroupAndNotify{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"AAAAAA");
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"BBBBBB");
    });
    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"CCCCCC");
    });
    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"DDDDDD");
    });
}

4.0 利用 NSOperation 利用依赖

-(void)doDependency{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *opInvocation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskopInvocation1) object:nil];
    NSInvocationOperation *opInvocation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskopInvocation2) object:nil];
    NSBlockOperation *opBlock1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"CCCCCC");
    }];
    NSBlockOperation *opBlock2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"DDDDDD");
    }];
    
    [opBlock1 addDependency:opInvocation1];
    [opBlock1 addDependency:opInvocation2];
    [opBlock2 addDependency:opInvocation1];
    [opBlock2 addDependency:opInvocation2];
    
    [queue addOperation:opInvocation1];
    [queue addOperation:opInvocation2];
    [queue addOperation:opBlock1];
    [queue addOperation:opBlock2];
    
}
-(void)taskopInvocation1{
    NSLog(@"AAAAA");
}
-(void)taskopInvocation2{
    NSLog(@"BBBBBB");
}

5.0 利用 NSOperation中的优先级属性:
这里需要注意两点:
任务的执行顺序除了和优先级有关系,还和依赖有关系,并且依赖是高于优先级的,什么意思呢,C 依赖于 B,C的优先级高于B,依然是B先执行。
优先级确定的是谁先开始执行,而非谁先结束执行!!!!

-(void)doPriority{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *opInvocation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskopInvocation1) object:nil];
    NSInvocationOperation *opInvocation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskopInvocation2) object:nil];
    NSBlockOperation *opBlock1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"CCCCCC");
    }];
    NSBlockOperation *opBlock2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"DDDDDD");
    }];
    
    opInvocation1.queuePriority = NSOperationQueuePriorityHigh;
    opInvocation2.queuePriority = NSOperationQueuePriorityHigh;
    opBlock1.queuePriority = NSOperationQueuePriorityNormal;
    opBlock2.queuePriority = NSOperationQueuePriorityNormal;
    [queue addOperation:opInvocation1];
    [queue addOperation:opInvocation2];
    [queue addOperation:opBlock1];
    [queue addOperation:opBlock2];
}
-(void)taskopInvocation1{
    NSLog(@"AAAAA");
}
-(void)taskopInvocation2{
    NSLog(@"BBBBBB");
}
上一篇下一篇

猜你喜欢

热点阅读