ios-最大并发数

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSOperationQueue * queue;
@end

@implementation ViewController
-(NSOperationQueue *)queue
{
    if(!_queue)
    {
        //初始化队列
        _queue=[[NSOperationQueue alloc]init];
        //设置最大并发数,不是说代表我只开两个线程,因为线程之间是相互切换的,万一你两个线程中的一个线程进入了就绪状态,那我肯定还需要一个线程去继续完成任务。
        _queue.maxConcurrentOperationCount=2;
    }
    return _queue;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    for(int i=0;i<10;i++)
    {
        [self.queue addOperationWithBlock:^{
            NSLog(@"%d %@",i,[NSThread currentThread]);
        }];

    }
}

此处开启了三个线程

//输出队列的操作数 
NSLog(@"%zd",self.queue.operationCount);
  //继续操作
  self.queue.suspended=NO;
 //暂停操作,当前正在执行的操作会执行完毕,后续的操作会暂停
   self.queue.suspended=YES;
    //取消所有操作,会把操作从队列中全部移除掉
        [self.queue cancelAllOperations]; 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值