IOS学习 NSOperation 并发数量、暂停/继续、取消、等待、依赖 与GCD区别

本文介绍了如何在iOS开发中使用NSOperation和GCD进行并发控制。通过创建队列、设置最大并发数、操作依赖、暂停/继续、取消任务等示例,展示了NSOperation的灵活性和功能。同时对比了NSOperation与GCD在并发控制上的不同,帮助开发者更好地理解和选择适合的并发机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@interface ViewController ()


//定义一个全局的队列

@property (nonatomic,strong)NSOperationQueue *opQueue;


@end


@implementation ViewController


//初始化对象

-(NSOperationQueue *)opQueue{

    if (_opQueue ==nil) {

        _opQueue = [[NSOperationQueuealloc]init];

    }

    return_opQueue;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 100, 40)];

    [btn setTitle:@"暂停/继续"forState:UIControlStateNormal];

    btn.backgroundColor = [UIColorpurpleColor];

    [btn addTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

    

    UIButton *btnStop = [[UIButtonalloc]initWithFrame:CGRectMake(100,200, 100,40)];

    [btnStop setTitle:@"取消全部"forState:UIControlStateNormal];

    btnStop.backgroundColor = [UIColorpurpleColor];

    [btnStop addTarget:selfaction:@selector(btnStop)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnStop];

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [selfdemo3];

}


-(void)demo1{

    //创建队列:相当于GCD的并发(全局)队列

//    NSOperationQueue *opq = [[NSOperationQueue alloc]init];

    

    //主队列:GCD主队列一致

    NSOperationQueue *opq =[NSOperationQueuemainQueue];

    

    //创建多个操作:相当于GCD的异步执行的任务

    for (int i =0; i < 20 ; i++) {

        NSOperation *op = [NSBlockOperationblockOperationWithBlock:^{

            NSLog(@"%@ %d",[NSThreadcurrentThread],i);

        }];

        [opq addOperation:op];

    }

}


-(void)demo2{

    NSOperationQueue *opq = [[NSOperationQueuealloc]init];

    

    //常用写法

    for (int i =0; i < 20 ; i++) {

        [opq addOperationWithBlock:^{

            NSLog(@"%@ %d",[NSThreadcurrentThread],i);

        }];

    }

    

    //NSBlockOperation

    NSBlockOperation *op1 = [NSBlockOperationblockOperationWithBlock:^{

        NSLog(@"op1 %@",[NSThreadcurrentThread]);}];

    [op1 addExecutionBlock:^{

        NSLog(@"op1-11111");

    }];

//    [opq addOperation:op1];

    

    //NSInvocationOperation 不常用

    NSOperation *op2 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(task:)object:@"Invocation"]; //多态的写法

//    [opq addOperation:op2];

    

    //依赖关系,可以跨队列,注意不要出现循环依赖

    [op2 addDependency:op1];

    

    //设置等待

    [opq addOperations:@[op1,op2]waitUntilFinished:YES];

    NSLog(@"等待执行");

    

    //线程间通信:在主线程里更新UI

    NSBlockOperation *op3 = [NSBlockOperationblockOperationWithBlock:^{

        NSLog(@"更新UI......%@",[NSThreadcurrentThread]);}];

    [[NSOperationQueuemainQueue] addOperation:op3];

    

    [op3 addDependency:op2]; //依赖关系,可以跨队列

}


-(void)task:(id)obj{

    NSLog(@"op2 %@ %@",[NSThreadcurrentThread],obj);

}


-(void)demo3{

    //设置最大并发数:不是线程的数量,而是同时执行操作的数量

    self.opQueue.maxConcurrentOperationCount = 2;

    

    for (int i =0; i < 20 ; i++) {

        NSOperation *bp = [NSBlockOperationblockOperationWithBlock:^{

            [NSThreadsleepForTimeInterval:1];

            NSLog(@"%@ %d",[NSThreadcurrentThread],i);

        }];

        [self.opQueueaddOperation:bp];

    }

}


//挂起:暂停和继续

-(void)btnAction{

    //判断操作数的数量

    if (self.opQueue.operationCount == 0) {

        NSLog(@"没有操作");

        return;

    }

    

    //暂停和继续:对队列的操作

    self.opQueue.suspended = !self.opQueue.suspended;    

    if (self.opQueue.suspended) {

        NSLog(@"暂停");

    }else{

        NSLog(@"继续");

    }

}


//取消队列里的所有操作,取消操作并不会影响队列的挂起状态

-(void)btnStop{

    [self.opQueuecancelAllOperations];

    NSLog(@"取消全部");

    

    //取消队列里的挂起状态

    self.opQueue.suspended =NO;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值