setMaxConcurrentOperationCount

2017-08-16  本文已影响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多少.

上一篇下一篇

猜你喜欢

热点阅读