首页 > 代码库 > 最大并发数

最大并发数

最大并发数

// 1.创建一个队列(非主队列)

    NSOperationQueue *queue = [[NSOperationQueuealloc]init];
   
   
// 2.设置最大并发(最多同时并发执行3个任务)
    queue.
maxConcurrentOperationCount= 3;
   
   
// 3.添加操作到队列中(自动异步执行任务,并发)
   
NSBlockOperation *operation1 = [NSBlockOperationblockOperationWithBlock:^{
       
NSLog(@"下载图片1---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation2 = [NSBlockOperationblockOperationWithBlock:^{
       
NSLog(@"下载图片2---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation3 = [NSBlockOperationblockOperationWithBlock:^{
       
NSLog(@"下载图片3---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation4 = [NSBlockOperationblockOperationWithBlock:^{
       
NSLog(@"下载图片4---%@", [NSThread currentThread]);
    }];
   
NSInvocationOperation *operation5 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(download)object:nil];
   
    [queue
addOperation:operation1];
    [queue
addOperation:operation2];
    [queue
addOperation:operation3];
    [queue
addOperation:operation4];
    [queue
addOperation:operation5];
    [queue
addOperationWithBlock:^{
       
NSLog(@"下载图片5---%@", [NSThread currentThread]);
    }];
    [queue
addOperationWithBlock:^{
       
NSLog(@"下载图片6---%@", [NSThread currentThread]);
    }];
    [queue
addOperationWithBlock:^{
       
NSLog(@"下载图片7---%@", [NSThread currentThread]);
    }];
    [queue
addOperationWithBlock:^{
       
NSLog(@"下载图片8---%@", [NSThread currentThread]);
    }];
    [queue
addOperationWithBlock:^{
       
NSLog(@"下载图片9---%@", [NSThread currentThread]);
    }];
   
    [queue
cancelAllOperations];

最大并发数