码迷,mamicode.com
首页 > 其他好文 > 详细

最大并发数

时间:2014-12-07 21:45:43      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:最大并发数

最大并发数

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

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
   
   
// 2.设置最大并发(最多同时并发执行3个任务)
    queue.
maxConcurrentOperationCount = 3;
   
   
// 3.添加操作到队列中(自动异步执行任务,并发)
   
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
       
NSLog(@"下载图片1---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
       
NSLog(@"下载图片2---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
       
NSLog(@"下载图片3---%@", [NSThread currentThread]);
    }];
   
NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
       
NSLog(@"下载图片4---%@", [NSThread currentThread]);
    }];
   
NSInvocationOperation *operation5 = [[NSInvocationOperation alloc] initWithTarget:self selector:@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];

最大并发数

标签:最大并发数

原文地址:http://blog.csdn.net/itcontend/article/details/41790799

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!