#pragma mark NSBlockOperation添加线程依赖
- (void)addDependency {
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operation1");
}];
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(3);
NSLog(@"operation2");
}];
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
sleep(5);
NSLog(@"operation3");
}];
[operation1 addDependency:operation2];
[operation2 addDependency:operation3];;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperations:@[operation1, operation2, operation3] waitUntilFinished:YES];
NSLog(@"end");
}
#pragma mark NSBlockOperation添加线程依赖
- (void)addDependency1 {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 2;
[queue addOperationWithBlock:^{
sleep(3);
NSLog(@"operation1");
}];
[queue addOperationWithBlock:^{
sleep(2);
NSLog(@"operation2");
}];
[queue addOperationWithBlock:^{
NSLog(@"operation3");
}];
NSLog(@"end");
}
网友评论