首页 > 代码库 > NSOperationQueue的maxConcurrentOperationCount属性

NSOperationQueue的maxConcurrentOperationCount属性

代码:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 2;

for (NSUInteger i = 1; i <= 10;  ++i) {
    [queue addOperationWithBlock:^{
        NSLog(@"Block %lu", i);
        [NSThread sleepForTimeInterval:1];
    }];
}

输出:

2016-11-03 22:20:37.508 maxConcurrentOperationCount[1196:26292] Block 1
2016-11-03 22:20:37.508 maxConcurrentOperationCount[1196:26295] Block 2
2016-11-03 22:20:38.512 maxConcurrentOperationCount[1196:26290] Block 3
2016-11-03 22:20:38.512 maxConcurrentOperationCount[1196:26295] Block 4
2016-11-03 22:20:39.517 maxConcurrentOperationCount[1196:26292] Block 5
2016-11-03 22:20:39.517 maxConcurrentOperationCount[1196:26290] Block 6
2016-11-03 22:20:40.523 maxConcurrentOperationCount[1196:26290] Block 8
2016-11-03 22:20:40.523 maxConcurrentOperationCount[1196:26295] Block 7
2016-11-03 22:20:41.525 maxConcurrentOperationCount[1196:26290] Block 9
2016-11-03 22:20:41.525 maxConcurrentOperationCount[1196:26292] Block 10

备注:

请注意每两行输出之间的时间间隔

NSOperationQueue的maxConcurrentOperationCount属性