星空网 > 软件开发 > 操作系统

多线程之NSOpertionQueue操作队列

//NSOpertionQueue  NSOperation  //Queue  //主队列 和 自定义队列  //主队列是运行在主线程当中,自定义队列运行在后台  //NSOperation 定义需要执行的操作(任务)  //定义需要的操作,然后把该操作添加到合适的队列中  //三个步骤  //1.创建队列对象  //2.创建操作对象  //3.把操作对象添加到队列之中,等待队列分配线程执行操作  //1.创建队列  NSOperationQueue *queue = [[NSOperationQueue alloc] init];  //设置最大并发操作数  //队列中最多有几个操作同时执行  queue.maxConcurrentOperationCount = 1;  //是否暂停执行队列中的线程  [queue setSuspended:YES];  //2.创建操作  //NSOperation 不能直接使用  //使用子类的对象 两种方式1、直接创建 2、使用block创建  NSOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self                              selector:@selector(thread1:)                               object:@"op1 "];    NSOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self                              selector:@selector(thread2:)                               object:@"op2 "];  /*  NSBlockOperation *op3 = [[NSBlockOperation alloc] init];  [op3 addExecutionBlock:^{    //具体要执行的操作  }];   */  //3.把操作加入到队列中  [queue addOperation:op1];  [queue addOperation:op2];  //加入之后,如果有操作,那队列就会自动执行  //4.设置操作的优先级  [op1 setQueuePriority:NSOperationQueuePriorityLow];  [op2 setQueuePriority:NSOperationQueuePriorityVeryHigh];  //5.设置操作之间的依赖关系  [op2 addDependency:op1];  //op2的执行依赖于op1,保证op1肯定op2之前执行  //是否重新让队列执行  [queue setSuspended:NO];   //回到主线程打印输出  for (int i = 0; i < 50; i ++) {    NSLog(@"主线程 : %d", i);  }}- (void)thread1:(NSString *)name{  //具体要执行的操作  for (int i = 0; i < 50; i ++) {    NSLog(@"多线程 %@:   %d", name, i);  }}- (void)thread2:(NSString *)name{  for (int i = 0; i < 50; i ++) {    NSLog(@"多线程 %@:   %d", name, i);  }}

 




原标题:多线程之NSOpertionQueue操作队列

关键词:线程

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

本土卖家占比96%!马来还未决定是否要求TikTok Shop办证;TTS Mall正式在该站上线;巴西将监管Shopee等纳税:https://www.kjdsnews.com/a/1668545.html
黑五今年还能迎来购物狂潮吗?:https://www.kjdsnews.com/a/1668546.html
Altman被“优化”后的96小时:https://www.kjdsnews.com/a/1668547.html
掌握亚马逊铺货的六大成功技巧!:https://www.kjdsnews.com/a/1668548.html
谷歌SEO专题|什么是内容短缺thin content?内容短缺的类型有哪些?:https://www.kjdsnews.com/a/1668549.html
TikTok Shop即将回归印尼:https://www.kjdsnews.com/a/1668550.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
如何用摄影作品表现“芳草鲜美,落英缤纷”的:https://www.vstour.cn/a/411247.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流