iOS中多线程相关方法——NSOperation及其相关类:

  NSOperation

  NSBlockOperation

  NSInvocationOperation

  NSOperationQueue

  1.NSOperation:一般在使用NSOperation进行线程操作时,不常用该类,而是用他的子类或NSOperationQueue;当然,也可以使用他的block属性创建线程。

 1 @interface ViewController ()
 2 {
 3 @public
 4     int count;
 5 }
 6 @end
 7
 8 @implementation ViewController
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11
12     NSLog(@"==Start==");
13
14     count = 0;
15     for (int i = 0; i < 10; i++) {
16         NSOperation *opration = [[NSOperation alloc] init];
17         opration.completionBlock = ^(){
18             NSLog(@"%@ = %@", @"operation_block", [NSThread currentThread]);
19         };
20         // 开启多线程
21         [opration start];
22     }
23
24     NSLog(@"==End==");
25 }
26
27 - (void)action:sender{
28     count ++;
29     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);
30 }
31 @end    

2016-04-18 10:10:42.563 Demo_NSOperation[1461:47803] ==Start==                                                              2016-04-18 10:10:42.564 Demo_NSOperation[1461:47803] ==End==                                                                2016-04-18 10:10:42.564 Demo_NSOperation[1461:47978] operation_block = <NSThread: 0x7fff32602320>{number = 2, name = (null)}2016-04-18 10:10:42.564 Demo_NSOperation[1461:47979] operation_block = <NSThread: 0x7fff3274a470>{number = 3, name = (null)}2016-04-18 10:10:42.564 Demo_NSOperation[1461:47982] operation_block = <NSThread: 0x7fff3240a940>{number = 5, name = (null)}2016-04-18 10:10:42.564 Demo_NSOperation[1461:47984] operation_block = <NSThread: 0x7fff32402e10>{number = 4, name = (null)}2016-04-18 10:10:42.565 Demo_NSOperation[1461:47978] operation_block = <NSThread: 0x7fff32602320>{number = 2, name = (null)}2016-04-18 10:10:42.565 Demo_NSOperation[1461:47979] operation_block = <NSThread: 0x7fff3274a470>{number = 3, name = (null)}2016-04-18 10:10:42.566 Demo_NSOperation[1461:47985] operation_block = <NSThread: 0x7fff32605a70>{number = 6, name = (null)}2016-04-18 10:10:42.566 Demo_NSOperation[1461:47982] operation_block = <NSThread: 0x7fff3240a940>{number = 5, name = (null)}2016-04-18 10:10:42.566 Demo_NSOperation[1461:47984] operation_block = <NSThread: 0x7fff32402e10>{number = 4, name = (null)}2016-04-18 10:10:42.567 Demo_NSOperation[1461:47978] operation_block = <NSThread: 0x7fff32602320>{number = 2, name = (null)}

  2.NSBlockOperation:是NSOperation的子类

 1 @interface ViewController ()
 2 {
 3     @public
 4     int count;
 5 }
 6 @end
 7
 8 @implementation ViewController
 9
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12
13     NSLog(@"==Start==");
14
15     count = 0;
16     for (int i = 0; i < 10; i++) { // 直接使用NSBlockOperation的类方法
17         NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
18             NSLog(@"%@ = %@", @"blockOperation", [NSThread currentThread]);
19         }];
20
21         [blockOperation start];
22     }
23
24     NSLog(@"==End==");
25 }
26
27 - (void)action:sender{
28     count ++;
29     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);
30 }
31 @end

2016-04-18 12:04:03.893 Demo_NSOperation[2103:104349] ==Start==                                                           2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.894 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] blockOperation = <NSThread: 0x7fb3f3502930>{number = 1, name = main}2016-04-18 12:04:03.895 Demo_NSOperation[2103:104349] ==End==                                                             

 1 @interface ViewController ()
 2 {
 3     @public
 4     int count;
 5 }
 6 @end
 7
 8 @implementation ViewController
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11
12     NSLog(@"==Start==");
13
14     count = 0;
15     for (int i = 0; i < 5; i++) {
16         NSBlockOperation *blockOperation = [NSBlockOperation new];
17         // 使用NSBlockOperation的实例方法
18         [blockOperation addExecutionBlock:^{
19             NSLog(@"%@ = %@", @"blockOperation1", [NSThread currentThread]);
20         }];
21
22         [blockOperation addExecutionBlock:^{
23             NSLog(@"%@ = %@", @"blockOperation2", [NSThread currentThread]);
24         }];
25
26         [blockOperation addExecutionBlock:^{
27             NSLog(@"%@ = %@", @"blockOperation3", [NSThread currentThread]);
28         }];
29
30         [blockOperation start];
31     }
32
33     NSLog(@"==End==");
34 }
35
36 - (void)action:sender{
37     count ++;
38     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);
39 }
40 @end

2016-04-18 14:07:23.518 Demo_NSOperation[2797:178037] ==Start==                                                              2016-04-18 14:07:23.520 Demo_NSOperation[2797:178037] blockOperation1 = <NSThread: 0x7f9019e02610>{number = 1, name = main}  2016-04-18 14:07:23.520 Demo_NSOperation[2797:178070] blockOperation3 = <NSThread: 0x7f9019f13620>{number = 2, name = (null)}2016-04-18 14:07:23.520 Demo_NSOperation[2797:178069] blockOperation2 = <NSThread: 0x7f9019c0bf50>{number = 3, name = (null)}2016-04-18 14:07:23.521 Demo_NSOperation[2797:178037] blockOperation1 = <NSThread: 0x7f9019e02610>{number = 1, name = main}  2016-04-18 14:07:23.521 Demo_NSOperation[2797:178070] blockOperation3 = <NSThread: 0x7f9019f13620>{number = 2, name = (null)}2016-04-18 14:07:23.521 Demo_NSOperation[2797:178069] blockOperation2 = <NSThread: 0x7f9019c0bf50>{number = 3, name = (null)}2016-04-18 14:07:23.522 Demo_NSOperation[2797:178037] blockOperation1 = <NSThread: 0x7f9019e02610>{number = 1, name = main}  2016-04-18 14:07:23.522 Demo_NSOperation[2797:178070] blockOperation3 = <NSThread: 0x7f9019f13620>{number = 2, name = (null)}2016-04-18 14:07:23.522 Demo_NSOperation[2797:178069] blockOperation2 = <NSThread: 0x7f9019c0bf50>{number = 3, name = (null)}2016-04-18 14:07:23.523 Demo_NSOperation[2797:178037] blockOperation1 = <NSThread: 0x7f9019e02610>{number = 1, name = main}  2016-04-18 14:07:23.523 Demo_NSOperation[2797:178070] blockOperation2 = <NSThread: 0x7f9019f13620>{number = 2, name = (null)}2016-04-18 14:07:23.523 Demo_NSOperation[2797:178069] blockOperation3 = <NSThread: 0x7f9019c0bf50>{number = 3, name = (null)}2016-04-18 14:07:23.524 Demo_NSOperation[2797:178037] blockOperation1 = <NSThread: 0x7f9019e02610>{number = 1, name = main}  2016-04-18 14:07:23.524 Demo_NSOperation[2797:178070] blockOperation3 = <NSThread: 0x7f9019f13620>{number = 2, name = (null)}2016-04-18 14:07:23.524 Demo_NSOperation[2797:178069] blockOperation2 = <NSThread: 0x7f9019c0bf50>{number = 3, name = (null)}2016-04-18 14:07:23.524 Demo_NSOperation[2797:178037] ==End==                                                                

  3.NSInvocationOperation:是NSOperation的子类

 1 @interface ViewController ()
 2 {
 3     @public
 4     int count;
 5 }
 6 @end
 7
 8 @implementation ViewController
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11
12     NSLog(@"==Start==");
13
14     count = 0;
15     for (int i = 0; i < 5; i++) {
16         // 使用构造方法
17         NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"invocationOperation"];
18         // 使用实例方法
19         [invocationOperation setCompletionBlock:^{
20             NSLog(@"%@ %@", @"iop", [NSThread currentThread]);
21         }];
22
23         [invocationOperation start];
24     }
25
26     NSLog(@"==End==");
27 }
28
29 - (void)action:sender{
30     count ++;
31     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);
32 }
33 @end

2016-04-18 14:18:41.245 Demo_NSOperation[2929:183996] ==Start==                                                                   2016-04-18 14:18:41.246 Demo_NSOperation[2929:183996] invocationOperation[1] = <NSThread: 0x7fe0c3506ed0>{number = 1, name = main}2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[2] = <NSThread: 0x7fe0c3506ed0>{number = 1, name = main}2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[3] = <NSThread: 0x7fe0c3506ed0>{number = 1, name = main}2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[4] = <NSThread: 0x7fe0c3506ed0>{number = 1, name = main}2016-04-18 14:18:41.247 Demo_NSOperation[2929:184167] iop <NSThread: 0x7fe0c3704110>{number = 2, name = (null)}                   2016-04-18 14:18:41.247 Demo_NSOperation[2929:183996] invocationOperation[5] = <NSThread: 0x7fe0c3506ed0>{number = 1, name = main}2016-04-18 14:18:41.248 Demo_NSOperation[2929:183996] ==End==                                                                     2016-04-18 14:18:41.248 Demo_NSOperation[2929:184170] iop <NSThread: 0x7fe0c3704180>{number = 4, name = (null)}                   2016-04-18 14:18:41.247 Demo_NSOperation[2929:184171] iop <NSThread: 0x7fe0c3417940>{number = 3, name = (null)}                   2016-04-18 14:18:41.248 Demo_NSOperation[2929:184167] iop <NSThread: 0x7fe0c3704110>{number = 2, name = (null)}                   2016-04-18 14:18:41.248 Demo_NSOperation[2929:184177] iop <NSThread: 0x7fe0c36093a0>{number = 5, name = (null)}                   

  无论是NSBlockOperation的类方法还是NSInvocationOperation实例方法都在主线程中执行;而使用addExecutionBlock方法则会在主线程及子线程中运行;使用setCompletionBlock方法则会开辟多线程。

  4.NSOperationQueue:并不是NSOperation的子类,但是常与NSOperation搭配使用,加入queue的操作会开辟子线程处理

 1 @interface ViewController ()
 2 {
 3     @public
 4     int count;
 5 }
 6 @end
 7
 8 @implementation ViewController
 9
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12
13     NSLog(@"==Start==");
14
15     count = 0;
16     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
17     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue"];
18     NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue1"];
19     NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"operationQueue2"];
20     [queue addOperations:@[operation, operation1, operation2] waitUntilFinished:YES];
21
22     NSLog(@"==End==");
23 }
24
25 - (void)action:sender{
26     count ++;
27     NSLog(@"%@[%d] = %@", sender, count, [NSThread currentThread]);
28 }
29 @end

2016-04-18 15:03:12.566 Demo_NSOperation[3259:201876] ==Start==                                                 2016-04-18 15:03:12.567 Demo_NSOperation[3259:202092] operationQueue2[2] = <NSThread: 0x7fa791c1fe60>{number = 4, name = (null)}2016-04-18 15:03:12.567 Demo_NSOperation[3259:202072] operationQueue[3] = <NSThread: 0x7fa791e05a80>{number = 3, name = (null)} 2016-04-18 15:03:12.567 Demo_NSOperation[3259:202070] operationQueue1[1] = <NSThread: 0x7fa791d48370>{number = 2, name = (null)}2016-04-18 15:03:12.568 Demo_NSOperation[3259:201876] ==End==                                                                   

  NSOperation中含有@property NSOperationQueuePriority queuePriority;@property NSQualityOfService qualityOfService;的枚举属性,可选择线程的优先级以及子线程开辟方式。

转载于:https://www.cnblogs.com/kriskee/p/5404564.html

浅谈多线程——NSOperation相关推荐

  1. 浅谈多线程——NSThread

    上一篇文章中我们大致了解了GCD的模式和方法,在iOS开发中除了GCD之外,还有NSThread和NSOperation两种多线程方式. 1.NSThread - a - 使用NSThread开辟多线 ...

  2. 为什么cpu要一心二用:浅谈多线程编程的一个具体例子

    什么是多线程,多线程就是多个执行序列.就是让cpu执行下这个序列,又执行下那个序列,不停地切换, 正所谓一心二用. 考虑一个实际场景: 客户端要向服务端发送文件,服务端会对发送文件进行检查,如果文件的 ...

  3. 浅谈多核CPU、多线程与并行计算

    浅谈多核CPU.多线程与并行计算 xiaofei0859 2017-05-09 17:07:11  3646  收藏 展开 0.前言 笔者用过MPI和C#线程池,参加过比赛,有所感受,将近一年来,对多 ...

  4. 多线程之旅之四——浅谈内存模型和用户态同步机制

     用户态下有两种同步结构的 volatile construct: 在简单数据类型上原子性的读或者写操作   interlocked construct:在简单数据类型上原子性的读和写操作 (在这里还 ...

  5. java 多线程同步_浅谈Java多线程(状态、同步等)

    Java多线程是Java程序员必须掌握的基本的知识点,这块知识点比较复杂,知识点也比较多,今天我们一一来聊下Java多线程,系统的整理下这部分内容. 一.Java中线程创建的三种方式: 1.通过继承T ...

  6. js打印线程id_浅谈python中的多线程和多进程(二)

    原创:hxj7 本文继续分享一个关于python多线程和多进程区别的例子 前文<浅谈python中的多线程和多进程>中我们分享过一个例子,就是分别利用python中的多线程和多进程来解决高 ...

  7. linux的多任务 多进程,浅谈linux模拟多线程崩溃和多进程崩溃

    结论是: 多线程下如果其中一个线程崩溃了会导致其他线程(整个进程)都崩溃: 多进程下如果其中一个进程崩溃了对其余进程没有影响: 多线程 #include #include #include #incl ...

  8. 获得进程id_浅谈python中的多线程和多进程(二)

    原创:hxj7 本文继续分享一个关于python多线程和多进程区别的例子 前文<浅谈python中的多线程和多进程>中我们分享过一个例子,就是分别利用python中的多线程和多进程来解决高 ...

  9. 【kafka】浅谈Kafka的多线程消费的设计

    1.概述 转载:浅谈Kafka的多线程消费的设计 看原文去... 一.前言 跟RabbitMQ相比,Kafka的分区机制(Partition)使其支持对同一个"队列"分片并行读取, ...

最新文章

  1. 插件化知识储备-Binder和AIDL原理
  2. **Python垃圾回收机制
  3. 自己定义九宫格手势解锁
  4. Loadrunner学习笔记(四)
  5. [JS 分析] 天_眼_查 字体文件
  6. Linux第三周作业
  7. id vue2路由传参_Vue2.0中 $route 和 $router 的区别
  8. 机械设计说明书_如何做机械设计课程设计?这篇文章总结很详细
  9. 炸了!!又一 VSCode 神器面世!
  10. jsp过滤器一点小结
  11. 基于web的电影院订票系统(毕设系统,到手可用)
  12. 数据分析学习总结笔记12:空间自相关——空间位置与相近位置的指标测度
  13. RBF神经网络学习及实践
  14. 视频格式mkv、mp4、avi、flv、mov、wmv、webm特点和区别
  15. C#文件操作——文件、数据流与注册表
  16. springboot实现pdf打印和预览
  17. 计算机科学论文生成器,高考满分作文生成器来了!分分钟批量完成「生活在XX上」...
  18. 「维基解密」:西游记死亡人数
  19. 《JavaScript实现页面图片滚动播放》
  20. 用robot framework + python实现http接口自动化测试框架

热门文章

  1. 基于SSM的学生选课系统
  2. matlab 求矩阵秩,求Matlab中矩阵的秩和迹 | 学步园
  3. 南开大学计算机考研资料汇总
  4. postgre数据库字符串函数和日期函数操作小记
  5. aspose pdf java,Java 使用aspose.pdf将多张图片转成pdf的方法及示例代码
  6. OFDM系统仿真【matlab代码】
  7. bosun 快速入门
  8. MPU6500模块调试
  9. 【读点论文】Swin Transformer: Hierarchical Vision Transformer using Shifted Windows通过窗口化进行局部MSA,sw-MSA融合信息
  10. python测试脚本 进制转换器_进制转换(用Python实现进制转换器)