一、GCD
dispatch_semaphore_t sephore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"222");
dispatch_semaphore_wait(sephore, DISPATCH_TIME_FOREVER);
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"123");
dispatch_semaphore_signal(sephore);
});
注意:dispatch_semaphore_wait(sephore, DISPATCH_TIME_FOREVER);不能写在主线程中,会卡死的,一直等待。
输出结果: 123
222
一、NSOperation
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
NSBlockOperation * o1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"o1 finish");
}];
NSBlockOperation * o2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"o2 finish");
}];
[op1 addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];
输出结果: o2 finish
o1 finish