美文网首页
NSBlockOperation添加线程依赖

NSBlockOperation添加线程依赖

作者: Jean_Lina | 来源:发表于2020-11-24 16:59 被阅读0次
    #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");
    }
    
    

    相关文章

      网友评论

          本文标题:NSBlockOperation添加线程依赖

          本文链接:https://www.haomeiwen.com/subject/pfxwiktx.html