美文网首页
setMaxConcurrentOperationCount

setMaxConcurrentOperationCount

作者: 晓叶 | 来源:发表于2017-08-16 14:27 被阅读39次

    针对有些人把setMaxConcurrentOperationCount说成是线程的多少,做下面代码测试.

    {
        NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
            
            
            for (int i = 0; i<5;i++)
            {
                NSLog(@"%@:i=%d",[NSThread currentThread] ,i);
            }
        }];
        
        [op1 addExecutionBlock:^{
            
            for (int m = 0; m<5;m++)
            {
                NSLog(@"%@:m=%d",[NSThread currentThread],m);
            }
        }];
        
        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
            
            
            for (int j = 0; j<5;j++)
            {
                NSLog(@"%@:j=%d",[NSThread currentThread] ,j);
            }
        }];
        [op2 addExecutionBlock:^{
            
            for (int k = 0; k<5;k++)
            {
                NSLog(@"%@:k=%d",[NSThread currentThread],k);
            }
        }];
        
        NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
            
            
            for (int n = 0; n<5;n++)
            {
                NSLog(@"%@:n=%d",[NSThread currentThread] ,n);
            }
        }];
        [op3 addExecutionBlock:^{
            
            for (int q = 0; q<5;q++)
            {
                NSLog(@"%@:q=%d",[NSThread currentThread],q);
            }
        }];
        
        
        
        
        NSOperationQueue *q = [[NSOperationQueue alloc]init];
        [q setMaxConcurrentOperationCount:2];
        [q addOperation:op1];
        [q addOperation:op2];
        [q addOperation:op3];
        [q setSuspended:NO];
    }
    
    setMaxConcurrentOperationCount=1.png setMaxConcurrentOperationCount=2.png

    总结:上面分别是setMaxConcurrentOperationCount为1和2时程序输出的数据,可以看出setMaxConcurrentOperationCount表示的是并不是线程的多少,而是操作的NSBlockOperation多少.

    相关文章

      网友评论

          本文标题:setMaxConcurrentOperationCount

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