1,NSoperationQueue处理A,B,C三个线程,要求执行完A,B后才能执行C。
添加依赖关系,代码:
- (void)operation { NSOperationQueue *queue = [[NSOperationQueue alloc] init]; NSBlockOperation *A = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"A----%@",[NSThread currentThread]); }]; NSBlockOperation *B = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"B---%@",[NSThread currentThread]); }]; NSBlockOperation *C = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"C---%@",[NSThread currentThread]); }]; [C addDependency:A]; [C addDependency:B]; [queue addOperation:A]; [queue addOperation:B]; [queue addOperation:C]; }
2,使用GCD的栅栏函数或者队列组
- (void)combineImages { /* 1,开启两个线程处理A和B 2,通过任务组执行A,B之后执行C */ dispatch_queue_t queue = dispatch_queue_create("dealWith", DISPATCH_QUEUE_CONCURRENT); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ NSLog(@"A----%@",[NSThread currentThread]); }); dispatch_group_async(group, queue, ^{ NSLog(@"B---%@",[NSThread currentThread]); }); dispatch_group_notify(group, queue, ^{ NSLog(@"C---%@",[NSThread currentThread]); }); }
3,GCD添加栅栏函数
- (void)GCDasyn { //栅栏函数(栅栏函数不能用全局并发队列) //等执行完栅栏函数中的代码蔡继续执行下面的代码 dispatch_queue_t queue = dispatch_queue_create("dealWith", DISPATCH_QUEUE_CONCURRENT); dispatch_barrier_async(queue, ^{ NSLog(@"A----%@",[NSThread currentThread]); }); dispatch_barrier_async(queue, ^{ NSLog(@"B---%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"C---%@",[NSThread currentThread]); }); }