博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程之NSOpertionQueue操作队列
阅读量:7072 次
发布时间:2019-06-28

本文共 1842 字,大约阅读时间需要 6 分钟。

//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);    }}

 

转载于:https://www.cnblogs.com/sunjiachen/p/4791542.html

你可能感兴趣的文章
网页锚点链接
查看>>
Linux运维 第五阶段 puppet基础
查看>>
【自学Linux】Linux文件系统管理(二)
查看>>
我的友情链接
查看>>
二手交易市场将慢慢落幕还是繁华?
查看>>
DNS
查看>>
android之路重新开启
查看>>
linux设置时间服务器
查看>>
NetScaler配置证书自签发
查看>>
Virtio:针对 Linux 的 I/O 虚拟化框架
查看>>
mysql 安装
查看>>
python函数:zip()
查看>>
我的友情链接
查看>>
联机对战DEMO
查看>>
使用struct实现面向对象编程的封装
查看>>
2017.12.20 2周3次课
查看>>
oracle O7_DICTIONARY_ACCESSIBILITY 参数
查看>>
C++ Flash之间本地通讯
查看>>
C#.net数据库访问及其操作类
查看>>
微软大中华区副总裁:全线投入云计算领域
查看>>